Routes in Angular Not working - angularjs

Code
<body ng-app="ngapp">
<h2>NG App</h2>
<div>
<ng-view></ng-view>
</div>
<script src="Scripts/angular.min.js"></script>
<script>
var app = angular.module('ngapp', ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'Page1.html',
controller: 'simpCtrl'
})
.when('/view2', {
templateUrl: 'Page2.html',
controller: 'simpCtrl'
});
});
app.controller('simpCtrl', function ($scope) {
$scope.customers = [
{ name: 'Jack', age: 10 },
{ name: 'Abdul', age: 12 },
{ name: 'Zubair', age: 11 },
{ name: 'Ammar', age: 10 }
];
});
</script>
I have registered the routes properly but its is not working, I don't know why. Any Idea how to debug or find misbehaving code in angular?
Page1.html
<div>
<h2>View 1</h2>
Customer Name:
<input type="text" ng-model="search.name" />
<ul>
<li ng-repeat="n in customers | orderBy:'name' | filter:search:strict ">{{n.name|uppercase}} - {{n.age}}
</li>
</ul>
View 2
Thanks
Plunker link to code

There are few issues with your application,
(i) You are refering to angular2 library, change it like this
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.6/angular.js"></script>
(ii) you have not added reference for ngRoute
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.4/angular-route.js"></script>
Working Application

Related

angularJs routeProvider issue

I have the below mentioned Angular code that uses the config and the routeProvider. View1 and View 2 are also enclosed.
<!DOCTYPE html>
<html>
<head>
<title>My test application</title>
</head>
<body ng-app="myApp">
<div>
<div ng-view></div>
</div>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-
min.js"></script>
<script
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-
route.js"></script>
<script>
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function($routeProvider){
$routeProvider
.when('/',
{
controller: 'SimpleController',
templateUrl: 'Partials/view1.html'
})
.when('/view2',{
controller: 'SimpleController'
templateUrl:'Partials/view2.html'
})
.otherwise({
redirectTo: '/'
});
});
myApp.controller('SimpleController',SimpFunc);
function SimpFunc($scope){
$scope.customers = [
{name:'Dave Smith', city:'New York'},
{name:'Will Smith', city:'Phily'},
{name:'Will Die', city:'Cincy'},
{name:'Die till', city:'New Jersey'},
{name:'Till What', city:'Cincy'}
];
$scope.addCustomer = function(){
$scope.customers.push(
{
name: $scope.newCust.name,
city: $scope.newCust.city
});
};
}
</script>
</body>
</html>
View1.html
<div class="container">
<h2>View 1</h2>
Name:
<br />
<input type="text" ng-model="filter.name" />
<br />
<ul>
<li ng-repeat="cust in customers | filter:filter.name">{{ cust.name
}} - {{ cust.city }}</li>
</ul>
<br />
Customer Name:
<br />
<input type="text" ng-model="newCust.name" />
Customer City:
<br />
<input type="text" ng-model="newCust.city" />
<br />
<button ng-click="addCustomer()">Add customer</button>
<br />
View 2
</div>
View 2
<div class="container">
<h2>View 2</h2>
City:
<br />
<input type="text" ng-model="filter.city" />
<br />
<ul>
<li ng-repeat="cust in customers | filter:filter.city">{{ cust.name
}} - {{ cust.city }}</li>
</ul>
</div>
I am not able to render view1 and/or view2 in the page. How can I do that?
Your script library URLs are giving 404 error.
I used below
https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular.js
https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.4/angular-route.js
And comma missing after SimpleController
.when('/view2',{
controller: 'SimpleController',
you have some syntax error in your app.config, also you have to make ng-href instead href for example <a ng-href="#!/view2">View 2</a>, replace your codes with below codes.
also you miss define the controller in the Html
var myApp = angular.module('myApp', ['ngRoute']);
myApp.config(function ($routeProvider) {
$routeProvider.when('/',
{
controller: 'SimpleController',
templateUrl: 'partials/view1.html'
})
.when('/view2',
{
controller: 'SimpleController',
templateUrl: 'partials/view2.html'
})
.otherwise({
redirectTo: '/'
});
});
myApp.controller('SimpleController', function ($scope) {
$scope.customers = [
{ name: 'Dave Smith', city: 'New York' },
{ name: 'Will Smith', city: 'Phily' },
{ name: 'Will Die', city: 'Cincy' },
{ name: 'Die till', city: 'New Jersey' },
{ name: 'Till What', city: 'Cincy' }
];
$scope.addCustomer = function () {
$scope.customers.push(
{
name: $scope.newCust.name,
city: $scope.newCust.city
});
};
});
<div ng-app="myApp" ng-controller="SimpleController">
<div ng-view></div>
</div>

Routing issue in Angular JS

i am just new to angular js and when I tried to study routing one error occurs..
please check this..
here is my index.html
<!DOCTYPE html>
<html data-ng-app="demoApp">
<head>
<title>Angular</title>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.0.4/css/bootstrap-combined.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div data-ng-view=""> </div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script>
var demoApp = angular.module('demoApp', []);
demoApp.controller('firstController', function($scope) {
$scope.customers = [{
name: 'nik',
city: 'san fransisco'
}, {
name: 'henry',
city: 'new york'
}, {
name: 'avida',
city: 'california'
}, {
name: 'lynda',
city: 'texas'
}];
$scope.addCust = function($scope) {
$scope.customers.push({
name: $scope.newCust.name,
city: $scope.newCust.city
})
}
});
demoApp.config(function($routeProvider) {
$routeProvider
.when('/view1', {
controller: 'firstController',
templateUrl: '1.html'
})
.when('/view2', {
controller: 'firstController',
templateUrl: '2.html'
})
.otherwise({
redirectTo: '/view1'
});
});
</script>
</body>
</html>
routing is not working, as it supposed to load "/view2" as default when i take index.html
I hope no need to include code from 1.html and 2.html here, for reference as that just contains the 'li' tag which shows the data from this.
Error is,
angular.js:38Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.5.6/$injector/modulerr?p0=demoApp&p1=Error%3A…ogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.5.6%2Fangular.min.js%3A39%3A222)
Thank.
you must include ng-route script angular-route

Run route angularjs without server

I need to run this router without nodejs server. Is it possible. All other files for routing having in the same directory.But it is not routing with the certain url to desired pages. Also the first page 'online_exam_1.html' content is not displaying while I am open this page with browser.
<html data-ng-app="demoApp" >
<head>
<title>
Online Exam Syatem
</title>
</head>
<body ><div class="container" ng-controller="RoutingController">
<div>
<h3>Adding Module Configuration and Routing</h3>
<!-- ng-view handles loading partials into it based
upon routes -->
<div data-ng-view></div>
</div>
</div>
<script src="angular.min.js">
<script src="angular_route.js"> </script>
</script>
<script>
demoApp.config(function ($routeProvider) {
alert('in');
$routeProvider
.when('/',
{
controller: 'RoutingController',
templateUrl: urlBase + 'online_exam_1.html'
})
//Define a route that has a route parameter in it (:customerID)
.when('/2',
{
controller: 'RoutingController',
templateUrl: urlBase + 'online_exam_2.html'
})
.otherwise({ redirectTo: '/partial1' });
});
demoApp.controller('RoutingController', function ($scope) {
$scope.customers = [
{ name: 'Dave Jones', city: 'Phoenix' },
{ name: 'Jamie Riley', city: 'Atlanta' },
{ name: 'Heedy Wahlin', city: 'Chandler' },
{ name: 'Thomas Winter', city: 'Seattle' }
];
});
</script>
</body>
</html>

Issue with setting up simple AngularJS MVC

I have set up a AngularJS MVC structure. Unfortunately, there is no specific errors or warnings in the script. However, the screen is displaying nothing and views is not redering according to the URLS give in the address bar.
Please help me to fix this issue.
Please see the below codes sepearate
-----------------controller.js---------------------
var demoApp = angular.module('demoApp', []);
var controllers = {};
demoApp.controller('simpleController', function ($scope) {
$scope.customers = [{
name: 'Rex',
city: 'Kensas'
}, {
name: 'Cyrin',
city: 'Texas'
}, {
name: 'Vijith',
city: 'Florida'
}];
$scope.addCustomer = function () {
$scope.customers.push({
name: $scope.newCustomer.name,
city: $scope.newCustomer.city
});
$scope.newCustomer.name = '';
$scope.newCustomer.city = '';
};
});
demoApp.config(function ($routeProvider) {
$routeProvider
.when('/view1', {
controller: 'simpleController',
templateUrl: 'Partials/view1.html'
})
.when('/view2', {
controller: 'simpleController',
templateUrl: 'Partials/view2.html'
})
.otherwise({
redirectTo: '/view1'
});
});
-----------------------index.html--------------------------
<!doctype html>
<html ng-app="demoApp">
<head>
<script src="angular.min.js"></script>
<script src="controller.js"></script>
</head>
<body>
<div>
<div ng-view=""></div>
</div>
</body>
</html>
-------------Partials/view1.html----------------------------
<div class="container">
<h2>View 1</h2>
Name:
<br />
<input type="text" ng-model="name" />
<br />
<ul>
<li ng-repeat="cust in customers | filter:name">{{ cust.name }}</li>
</ul>
<br />
User Name:<br />
<input type="text" ng-model="newCustomer.name" />
<br />
User Name:<br />
<input type="text" ng-model="newCustomer.city" />
<br />
<button ng-click="addCustomer()">Add Customer</button>
<br />
View 2
</div>
Not including view2.html as it is less important here.
Hoping for the quick fix. :)
Thanks for your time!
Rex
Which version of Angular you are using?
If you are using 1.2* then you have to explicitly inject ngRoute module into your app by including angular-route.js separately
See docs: https://docs.angularjs.org/guide/migration#ngroute-has-been-moved-into-its-own-module
Import ngRoute in your code
var demoApp = angular.module('demoApp', ['ngRoute']);
Also you have not defined your controller to any of your HTML elements.
<div ng-controller="simpleController"></div>
DEMO PLUNKER
Note: Before you ask anything see your browser's console for any errors. If you haven't already included ngRoute, it throws an error you can see it in console.
you have to define your ng-controller
<div ng-controller="simpleController">
You have defined your app (ng-app).. But you have not defined your controller in any of your HTML files.
For the HTML file where you need to use the controller, call it there via:
<div ng-controller="simpleController">
// The rest of your HTML
</div>

Unable to route in Angularjs

I have been leaning AngularJS from e-book of Dan Wahlin. There author tried to explain about Modules, Route and Factories from the page 53. Then, I wrote the code in index.html as:
<div class="container" data-ng-app="demoApp">
<div>
<h3>Adding Module Configuration and Routing</h3>
<!-- ng-view handles loading partials into it based
upon routes -->
<div data-ng-view=""></div>
</div>
</div>
<script type="text/javascript" src="js/angular.js"></script>
<script type="text/javascript">
var demoApp = angular.module('demoApp', []);
demoApp.config(function ($routeProvider) {
$routeProvider
.when('/view1',
{
controller: 'SimpleController',
templateUrl: urlBase + 'View1.html'
})
//Define a route that has a route parameter in it (:customerID)
.when('/view2',
{
controller: 'SimpleController',
templateUrl: urlBase + 'View2.html'
})
.otherwise({ redirectTo: '/View1' });
});
demoApp.controller('SimpleController', function ($scope) {
$scope.customers = [
{ name: 'Dave Jones', city: 'Phoenix' },
{ name: 'Jamie Riley', city: 'Atlanta' },
{ name: 'Heedy Wahlin', city: 'Chandler' },
{ name: 'Thomas Winter', city: 'Seattle' }
];
});
$scope.addCustomer = function () {
$scope.customers.push({ name: $scope.newCustomer.name, city: $scope.newCustomer.city });
}
</script>
Then I created two html pages - View1.html and View2.html and in the View1.html, I wrote:
<div class="container">
<h3>View 1</h3>
Name<br />
<input type="text" data-ng-model="filter.name" />
<br />
<ul>
<li data-ng-repeat="cust in customers | filter:filter.name">
{{ cust.name }} - {{ cust.city }}
</li>
</ul>
<br />Customer Name<br />
<input type="text" data-ng-model="newCustomer.name" />
<br />
<br />Customer City<br />
<input type="text" data-ng-model="newCustomer.city" />
<br />
<button data-ng-click="addCustomer()">Add Customer</button>
<br />
View 2
</div>
When I browse as http://localhost/angular/index.html#/view1 , I do not see anything that's mean no output. I do not understand actually where I did wrong.
The biggest issue here is that you are not including the routing module, which is required since Angular 1.2+
<script type="text/javascript" src="js/angular.js"></script>
<script type="text/javascript" src="js/angular-route.js"></script>
<script type="text/javascript">
var demoApp = angular.module('demoApp', ['ngRoute']);
//Snip...
</script>
As mentioned by goutham in the comments, you also need to define urlBase which you do not appear to do anywhere.
Because JavaScript does type coercion for string concatenation, you are going to end up with the word 'undefined' appended to your templateUrl.
console.log(undefined + "view1.html"); // 'undefinedview1.html'

Resources