I am playing with angularjs, and I cannot find the reason why ng-view does not work. What am I doing wrong?
var app = angular.module('Demo', ['ngRoute'])
.config(function ($routeProvider) {
$routeProvider.when('#/about', {
templateUrl: 'about.html',
controller: 'homeController'
});
$routeProvider.when('#/contacts', {
template: 'contacts.html',
controller: 'contactsController'
});
});
app.controller('homeController', function ($scope) {
alert('homeController');
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src=""//ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js""></script>
<body ng-app>
<div class="menu">
<ul>
<li>About</li>
<li>Contacts</li>
</ul>
</div>
<div class="MainContent">
<div ng-view></div>
</div>
<template id="about.html">
about
</template>
<template id="contacts.html">
contacts
</template>
</body>
There were a few issues here:
Your script source for angular-route was incorrect in the HTML portion.
Your $routeProvider.when lines do not need '#', so I removed them.
One of your templates used templateUrl, which was not correct. It should only be template: as you are not calling a URL.
Here is a working plunker:
https://plnkr.co/edit/GSoJ4sAxM8joH6zmrxjf?p=preview
var app = angular.module('demo', ['ngRoute'])
// URLs should not have # in them
.config(function ($routeProvider) {
$routeProvider.when('/about', {
template: 'about.html',
controller: 'homeController'
});
$routeProvider.when('/contacts', {
template: 'contacts.html',
controller: 'contactsController'
});
});
app.controller('homeController', function ($scope) {
alert('homeController');
});
app.controller('contactsController', function ($scope) {
alert('contactsController');
});
Related
I have been following the tutorial:
https://thinkster.io/angular-rails#angular-routing
I have not done any rails integration yet, the question is specifically to angular.
When I do the hello worlds from the MainCtrl without using the router, everything works. When I use the router, I cannot get the inline angular template to display in my html page. Where is the error here?
app.js:
angular.module('flapperNews', ['ui.router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
$urlRouterProvider.otherwise('home');
}])
angular.module('flapperNews', [])
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world';
}]);
index.html:
<html>
<head>
<title>My Angular App</title>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="flapperNews">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<ui-view></ui-view> <!-- this is supposed to display the template below but it shows nothing -->
</div>
</div>
<script type="text/ng-template" id="/home.html">
<div class="page-header">
<h1>Flapper News</h1>
</div>
</script>
</body>
</html>
Your controller is recreating the module instead of referencing it. Change it like so:
angular.module('flapperNews')
.controller('MainCtrl', [
'$scope',
function($scope){
$scope.test = 'Hello world';
}]);
You're defining the 'flapperNews' module twice. Remove the second angular.module('flapperNews', []).
I am not able routing to other page what the problem I dont know please solve my problem.
I am using Eclipse Java EE IDE for Web Developers.
Version: Mars.1 Release (4.5.1)
Build id: 20150924-1200
// also include ngRoute for all our routing needs
var myApp = angular.module('myApp', ['ngRoute']);
// configure our routes
myApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('#/', {
templateUrl: 'home.html',
controller: 'mainController'
})
// route for the about page
.when('#/about', {
templateUrl: 'about.html',
controller: 'aboutController'
})
// route for the contact page
.when('#/contact', {
templateUrl: 'contact.html',
controller: 'contactController'
});
});
// create the controller and inject Angular's $scope
myApp.controller('mainController', function($scope) {
// create a message to display in our view
$scope.username = 'ranjeet';
$scope.password = 'singh';
$scope.message = 'Everyone come and see how good I look!';
});
myApp.controller('aboutController', function($scope) {
$scope.message = 'Look! I am an about page.';
});
myApp.controller('contactController', function($scope) {
$scope.message = 'Contact us! JK. This is just a demo.';
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-route.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="/">Angular Routing Example</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li>home
</li>
<li> About
</li>
<li>Contact
</li>
</ul>
</div>
</nav>
</header>
<div id="main">
<div ng-view></div>
</div>
</body>
</html>
file structure of my project :
[1
Your url values in $routeProvider.when() should not include # which will be set internally if applicable (as by default html5mode is disabled by angular router, so angular does use hash location strategy).
Try changing:
$routeProvider
// route for the home page
.when('#/', {
templateUrl: 'home.html',
controller: 'mainController'
})
To
$routeProvider
// route for the home page
.when('/', { //do remove # from all registered routes
// ^^ no "#"
templateUrl: 'home.html',
controller: 'mainController'
})
I have been attempting to get angular routing working and everytime I create a new project and It does not work. I have had it working in some projects but I can never see why my newly created project does not work.
Its probably something obvious, thanks for any help in advance.
<!DOCTYPE html>
<html ng-app="app">
<head>
<title></title>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/Styles.css" rel="stylesheet" />
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-route.min.js"></script>
</head>
<body>
<a href="#/">
<button class="btn btn-danger">Homepage</button></a>
<a href="#/about">
<button class="btn btn-success">About</button></a>
<a href="#/date">
<button class="btn btn-warning">Date</button></a>
<div class="row">
<div ng-view>
</div>
</div>
<script src="SinglePageApp/app.js"></script>
<script src="Scripts/bootstrap.min.js"></script>
</body>
</html>
app.js file
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
//default page
.when('/', {
templateUrl: "pages/homepage.html",
controller: 'HomeCtrl'
})
//about page
.when('/about', {
templateUrl: "pages/about.html",
controller: 'AboutCtrl'
})
//date page
.when('/date', {
templateUrl: "pages/date.html",
controller: 'DateCtrl'
});
});
app.controller('HomeCtrl', ['$scope', function ($scope) {
$scope.homepage = "Homepage";
}]);
app.controller('AboutCtrl', ['$scope', function ($scope) {
$scope.about = "Lorem ipsum............";
}]);
app.controller('DateCtrl', ['$scope', function ($scope) {
$scope.dateNow = new Date();
}]);
Try this plunker:
http://embed.plnkr.co/L6E4GCe3O0Jh1vqKyGFD/
I've used the example at the angularJS documentation to create your usecase.
You should change the template filepaths, with your own. I also haven't included bootstrap.
If you want to use buttons, then you can use this example in plunkr based on this answer by Josh David Miller(upvote him if you use his directive). Directives are a way to customize html, and here we're using one as an html attribute (you can also use them as standalone elements) to create a hyperlink button.
Here's fiddle for you that works as expected
Not sure why your code is not working, angular has pretty bad debugging tool.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular-route.js"></script>
<div>
<a href="#/">
<button class="btn btn-danger">Homepage</button>
</a>
<a href="#/about">
<button class="btn btn-success">About</button>
</a>
<a href="#/date">
<button class="btn btn-warning">Date</button>
</a>
<div class="row">
<div ng-view></div>
</div>
</div>
<script type="text/ng-template" id="pages/homepage.html">
{{homepage}}
</script>
<script type="text/ng-template" id="pages/about.html">
{{about}}
</script>
<script type="text/ng-template" id="pages/date.html">
{{dateNow}}
</script>
Script file looks like this
var app = angular.module('app', ['ngRoute']);
app.config(function ($routeProvider) {
$routeProvider
//default page
.when('/', {
templateUrl: "pages/homepage.html",
controller: 'HomeCtrl'
})
//about page
.when('/about', {
templateUrl: "pages/about.html",
controller: 'AboutCtrl'
})
//date page
.when('/date', {
templateUrl: "pages/date.html",
controller: 'DateCtrl'
})
.otherwise({redirectTo:'/'});
});
app.controller('HomeCtrl', ['$scope', function ($scope) {
$scope.homepage = "Homepage";
}]);
app.controller('AboutCtrl', ['$scope', function ($scope) {
$scope.about = "Lorem ipsum............";
}]);
app.controller('DateCtrl', ['$scope', function ($scope) {
$scope.dateNow = new Date();
}]);
angular.bootstrap(document.body, ['app']);
I am trying to figure out why I have this error:
Uncaught Error: [$injector:modulerr]
I have the following code in index.html
<!DOCTYPE html>
<html ng-app="MainApp">
<head>
<meta charset="utf-8">
<link rel='stylesheet' type='text/css' href='core/sty.css'>
<script src="core/CS/ang/angular.min.js"></script>
<script src="core/CS/ang/angular-resource.min.js"></script>
<script src="core/CS/ang/angular-route.min.js"></script>
</head>
<body>
<div id="header-cont">
<div id="header">
<ul style="list-style:none;">
<li style="display:inline;float:left;"><font style="font-size:38px;top:-5px;left:-1px;position:fixed;">title</font></li>
<li class="menu">News</li>
<li class="menu"><ul class="sub-menu">
<li class="SMoption">1 First login</li>
<li class="SMoption">2 Duble register</li>
</ul>account</li>
</ul>
</div>
</div>
<ng-view></ng-view>
<script src="core/CS/mainscript.js"></script>
<script src="core/CS/config.js"></script>
<script src="core/CS/controllers.js"></script>
<script src="core/CS/modules.js"></script>
</body>
</html>
The mainscript.js
'use strict';
var appv = angular.module("MainApp", ['ngRoute','ngResource']);
And config.js
'use strict';
appv.config(function($routeProvider) {
$routeProvider.
when('/', {
//controller: 'HomeController',
templateUrl: 'views/home.html'
}).
when('/page/:pgId', {
//controller: 'PageController',
templateUrl: 'views/page.html'
}).
when('/article/:pgId/:artId', {
//controller: 'ArticleController',
templateUrl: 'views/article.html'
}).
when('/admin/:adCode', {
//controller: 'AdminController',
templateUrl: 'views/admin.html'
}).
otherwise({templateUrl: 'views/home.html'});
$locationProvider.html5Mode(true);
});
As you can see I have included the ng-route.js, ngRoute module, but something doesn't work...
you enable the html5 mode by adding $locationProvider.html5Mode(true); but you didn't inject the $locationProvider provider into the config function, adding that will solve the problem :)
appv.config(function($routeProvider, $locationProvider ) {
.........
You need to include $locationProvider as a dependency in your config function like this:
'use strict';
appv.config(function($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider.
when('/', {
//controller: 'HomeController',
templateUrl: 'views/home.html'
}).
when('/page/:pgId', {
//controller: 'PageController',
templateUrl: 'views/page.html'
}).
when('/article/:pgId/:artId', {
//controller: 'ArticleController',
templateUrl: 'views/article.html'
}).
when('/admin/:adCode', {
//controller: 'AdminController',
templateUrl: 'views/admin.html'
}).
otherwise({templateUrl: 'views/home.html'});
});
I try to learn AngularJS. At the moment I focus on ui-Routing.
My Code is very simple, but it doenst work and I don't know why.
Description of my problem:
http://de.tinypic.com?ref=kd22ow.jpg
The browser shows:
"Hello World ui-Routing!" (h1 in index.html)
If I try to enter index.html/home.
It says: Error File not found!
Here is my Code:
index.html
<html>
<head>
<title>My Angular App!</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body ng-app="Test" ng-controller="MainCtrl">
<h1>Hello World ui-Routing!</h1>
<div ui-view>
</div>
</body>
</html>
app.js:
angular.module('Test', ['ui-router'])
.config([
'$stateProvider',
'$urlRouterProvider',
function($stateProvider, $urlRouterprovider)
{
$stateProvider
.state('home', {
url: '/home',
templateUrl: '/home.html',
controller: 'MainCtrl'
});
.state('test', {
url: '/test',
templateUrl: '/test.html',
controller: 'TestCtrl'
});
$urlRouterProvider.otherwise('home');
}])
.controller('MainCtrl', ['$scope',
function($scope){
$scope.test = "Hello Jan!";
});
.controller('TestCtrl', ['$scope',
function($scope){
$scope.test = "Hello Redirect!";
});
test.html:
<h2>Hello Test! </h2>
home.html:
<h2>Hello Home! </h2>
Where is my problem?
Looking forward to your answers,
Jan
You need to include the ui-router script in your html page too it is a third party and not included in angular.