Angular Routes not giving me any output - angularjs

Why my routes not working well ?
I'm sure that my code is true but no thing happen when I trying to run it , why the routes not routing the files ??
this is my main file code
routes.html
<!DOCTYPE html>
<html data-ng-app="MyApp">
<head>
<title>Routes</title>
</head>
<body>
<div >
<!-- my views -->
<div data-ng-view=""></div>
</div>
view 1
view 2
<!-- angular -->
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
<!-- routes -->
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
<!-- scope -->
<script>
var myApp = angular.module("MyApp",[]);
// define my routes
myApp.config(function($routeProvider){
$routeProvider.when('/',
{
templateUrl: 'view1.html',
controller: 'SimpleController'
});
$routeProvider.when('/view2',
{
templateUrl: 'view2.html',
controller: 'SimpleController'
});
$routeProvider.otherwise ({redirectTo: '/'});
});
// define my controller
myApp.controller("SimpleController", function($scope)
{
// my array
$scope.customers=[
{name:'astm',city:'Alex'},
{name:'tamer',city:'Usa'},
{name:'ahmed',city:'Cairo'}
];
// add new persons to my array
$scope.addPerson = function(){
$scope.customers.push(
{
name: $scope.newPerson.name,
city: $scope.newPerson.city
});
};
});
</script>
</body>
</html>
This is the first view file view1.html
<div class="container">
<h2>View 1</h2>
Name :
<br>
<input type="text" data-ng-model="filter.name">
<br>
<ul class="list-group">
<li class="list-group-item" data-ng-repeat="person in customers | filter:filter.name | orderBy:'name' ">
{{ person.name}} live in {{ person.city}}
</li>
</ul>
<br>
<h2>Add new person</h2>
Name :
<br>
<input type="text" data-ng-model="newPerson.name">
<br>
City :
<br>
<input type="text" data-ng-model="newPerson.city">
<br>
<button data-ng-click="addPerson()">Add person</button>
view 2
</div>
This is the second view file view2.html
<div class="container">
<h2>View 2</h2>
City :
<br>
<input type="text" data-ng-model="city">
<br>
<ul class="list-group">
<li class="list-group-item" data-ng-repeat="person in customers | filter:city | orderBy:'city' ">
{{ person.name}} live in {{ person.city}}
</li>
</ul>
view 1
</div>

You have not added dependency of ngRoute in your app ..
var myApp = angular.module("MyApp",['ngRoute']);
// define my routes
myApp.config(function($routeProvider){
$routeProvider.when('/',
{
templateUrl: 'view1.html',
controller: 'SimpleController'
});
$routeProvider.when('/view2',
{
templateUrl: 'view2.html',
controller: 'SimpleController'
});
$routeProvider.otherwise ({redirectTo: '/'});
});
// define my controller
myApp.controller("SimpleController", function($scope)
{
// my array
$scope.customers=[
{name:'astm',city:'Alex'},
{name:'tamer',city:'Usa'},
{name:'ahmed',city:'Cairo'}
];
// add new persons to my array
$scope.addPerson = function(){
$scope.customers.push(
{
name: $scope.newPerson.name,
city: $scope.newPerson.city
});
};
});
here is the working plunkr

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>

TypeError: Cannot read property '$$phase' of null - AngularJS

I'm learning AngularJS and trying to develop a simple app. The app consist of 2 pages(sections) which are list and details.
If I visit both page separately (manually enter the URL in the address bar), both pages loads fine without any error. But if I navigate to details page from list page, the console displays this error.
TypeError: Cannot read property '$$phase' of null
at Object.$$debounceViewValueCommit (angular.min.js:293)
at Object.$setViewValue (angular.min.js:293)
at Array.<anonymous> (angular.min.js:315)
at m.$digest (angular.min.js:145)
at m.$apply (angular.min.js:147)
at l (angular.min.js:101)
at XMLHttpRequest.N.onload (angular.min.js:106)
However, the page still load fine but the error bothers me. What exactly is happening?
app.js
var myApp = angular.module('myApp', ['ngRoute', 'heroControllers']);
myApp.config(['$routeProvider', function ($routeProvider) {
"use strict";
$routeProvider.
when('/list', {
templateUrl: 'partials/list.html',
controller: 'ListController'
}).
when('/details/:itemId', {
templateUrl: 'partials/details.html',
controller: 'DetailsController'
}).
otherwise({
redirectTo: '/list'
});
}]);
//controller
var heroControllers = angular.module('heroControllers', []);
heroControllers.controller('ListController', ['$scope', '$http', function ($scope, $http) {
"use strict";
$http.get('js/data.json').then(function (response) {
$scope.hero = response.data;
$scope.order = "nickname";
});
}]);
heroControllers.controller('DetailsController', ['$scope', '$http', '$routeParams', function ($scope, $http, $routeParams) {
"use strict";
$http.get('js/data.json').then(function (response) {
$scope.hero = response.data;
$scope.whichItem = $routeParams.itemId;
});
}]);
index.html
<!DOCTYPE html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Angular Demo</title>
<script src="lib/angular/angular.min.js"></script>
<script src="lib/angular/angular-route.min.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="page-header">
<h1>Heroes</h1>
</div>
<div ng-view></div>
</div>
</div>
</div>
</body>
</html>
list.html
<section>
<div class="form-group">
<input ng-model="query" type="text" class="form-control" placeholder="Search for heroes" autofocus>
</div>
<div class="form-group">
<label>Sort by: </label>
<select ng-model="order">
<option value="nickname">Nickname</option>
<option value="name">Name</option>
</select>
<input ng-model="direction" type="radio" name="direction" checked>
<label>Ascending</label>
<input ng-model="direction" type="radio" name="direction" value="reverse">
<label>Descending</label>
</div>
<ul class="list-group">
<li class="list-group-item" ng-repeat="item in hero | filter: query | orderBy: order:direction">
<a href="#!/details/{{hero.indexOf(item)}}">
<img ng-src="images/{{item.imagename}}.png" alt="Photo of {{item.nickname}}">
<div>
<h2>{{item.nickname | uppercase}}</h2>
<h3>{{item.name}}</h3>
</div>
</a>
</li>
</ul>
</section>
details.html
<section>
<div ng-model="hero" class="jumbotron">
<h2>{{hero[whichItem].nickname}}</h2>
<div>
<h3>{{hero[whichItem].name}}</h3>
<img ng-src="images/{{hero[whichItem].imagename}}.png" alt="Photo of {{hero[whichItem].imagename}}">
<div>{{hero[whichItem].lore}}</div>
</div>
</div>
« Back to search
</section>
data.json
[
{
"nickname" : "Sniper",
"imagename" : "sniper",
"name" : "Kardel Sharpeye",
"lore" : "Bla... Bla... Bla..."
},
{
"nickname" : "Io",
"imagename" : "io",
"name" : "Wisp",
"lore" : "Bla... Bla... Bla..."
},
{
"nickname" : "Timbersaw",
"imagename" : "timbersaw",
"name" : "Rizzrack",
"lore" : "Bla... Bla... Bla..."
}
]
Use stable version of angularjs(latest for now is 1.5.9) to get rid of
"TypeError: Cannot read property '$$phase' of null - AngularJS"
error.

View won't load in AngularJS

Don't see what I'm missing here. My javascript isn't throwing any errors, but when I run the main page all I get is a blank screen; it appears that view1.htm isn't loading.
Here's the main page:
<!doctype HTML>
<html ng-app="demoApp">
<!--- This is how you integrate with partials (views)... --->
<head>
<title>Using AngularJS Views</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-route.min.js"></script>
<!-- normally would put this in it's own js file and call with script src="" -->
<script>
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(function ($routeProvider) {
$routeProvider
.when("/",
{
controller:"simpleController",
templateURL:"partials/view1.htm"
})
.when("/view2",
{
controller:"simpleController",
templateURL:"partials/view2.htm"
})
.otherwise({ redirectTo: "/"});
});
demoApp.controller('simpleController', ['$scope', function($scope) {
$scope.customers = [
{name:"David Jones", city:"Phoenix"},
{name:"James 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>
</head>
<body>
<div>
<!-- placeholder for the views -->
<div ng-view></div>
</div>
</body>
</html>
and here's partials/view1.htm:
<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}}</li>
</ul>
<br/>
Customer Name:<br/>
<input type="text" ng-model="newCustomer.name" />
<br/>
Customer City:<br/>
<input type="text" ng-model="newCustomer.city" />
<br/>
<button ng-click="addCustomer()">Add Customer</button>
View 2
</div>
Thanks for any help.
Found it. templateURL should be templateUrl.

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'

AngularJS routes - views not loading

I am attempting to load two different partials into my page. The routes seem to be working. i.e i'm redirected to #/view1 by default, which is what I want. However when Partials/View1.html is not loaded into the page, and same is true when I manually navigate to #/view2. Can't seem to figure out what I am missing.
Here is index.html:
<!DOCTYPE html>
<html ng-app="demoApp">
<head>
<title>Using AngularJS Directives and Data Binding</title>
</head>
<body>
<div>
<div ng-view>
<!-- Placeholder for views -->
</div>
</div>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/angular-route.min.js"></script>
<script>
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/view1',
{
controller: 'SimpleController',
templateUrl: 'Partials/View1.html'
})
.when('/view2',
{
controller: 'SimpleController',
templateUrl: 'Partials/View2.html'
})
.otherwise({ redirectTo: '/view1' });
}]);
demoApp.controller('SimpleController', function ($scope) {
$scope.customers = [
{ name: 'John Smith', city: 'Phoenix' },
{ name: 'John Doe', city: 'New York City' },
{ name: 'Jane Doe', city: 'San Francisco' }
];
$scope.addCustomer = function() {
$scope.customers.push(
{
name: $scope.newCustomer.name,
city: $scope.newCustomer.city
});
};
});
</script>
</body>
</html>
and Partials/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 | orderBy: 'city'">{{ cust.name }} - {{ cust.city }}</li>
</ul>
<br>
Customer Name: <br>
<input type="text" ng-model="newCustomer.name">
<br>
Customer City: <br>
<input type="text" ng-model="newCustomer.city">
<br>
<button ng-click="addCustomer()">Add Customer</button>
<br>
View 2
</div>
And Partials/View2.html
<div class="container">
<h2>View 2</h2>
Name:
<br>
<input type="text" ng-model="city">
<br>
<ul>
<li ng-repeat="cust in customers | filter: name | orderBy: 'city'">{{ cust.name }} - {{ cust.city }}</li>
</ul>
</div>
Try it with a relative URL for the template: templateUrl: './Partials/View2.html'
EDIT (from comment):
XMLHttpRequests don't really work well locally - xmlhttprequest for local files, so your best bet is to install a webserver to host the content via http locally. apache is probably the easiest / most widely installed, although you can use node, nginx, etc..

Resources