I was wondering if someone could take a quick gander at my code and let me know if I am missing anything. I am trying to learn Angular.js, and have been using the famous Angular.js in 60ish minutes video. However the problem that I have is that angular has been updated since, and things are a little bit different. As a result I am having trouble orchestrating my routes properly. Much thanks to anyone in advance.
What I am really wondering is, could any body tell me what it is that I am missing in order to make these routes properly work?
This below is my scripts in my html. I followed the book, and as a result they are script tags within my index.html page.
var demoApp = angular.module('demoApp', ['helperModule']);
var controllers = {};
demoApp.controller("CustomersController", function ($scope){
$scope.customers = [
{name: 'Dave Jones', city: 'pheonix'},
{name: 'Dave Jones', city: 'New york'},
{name: 'Jones suman', city: 'pheonix'},
{name: 'Naresh babu', city: 'Hyderabad'}
];
});
var demoApp = angular.module('demoApp', ['ngRoute']);
demoApp.config(function($routeProvider){
$routeProvider
.when('/',
{
controller: "SimpleController",
templateUrl: "views/view1.html"
})
.when('partial2',
{
controller: "SimpleController"
templateUrl: "views/view2.html"
})
.otherwise({ redirectTo: "/"})
});
$scope.addCustomer = function(){
$scope.customers.push({name: $scope.newCustomer.name, city: $scope.newCustomer.city});
}
<script src = "angular-route.js"></script>
This is what I have for my view #1 and view#2 respectively. I feel like everything is correct, however I cannot get any of the names of the customers to appear.
<div cass = "container">
<h2>View 1</h2>
Name:
<input type = "text" data-ng-model="filter.name" />
<ul><li data-ng-repeat="cust in customers | filter:filter.name"></ul>
Customer Name: <br />
<input type= "text" data-ng-model="newCustomer.name" />
Customer City: <br />
<input type= "text" data-ng-model="newCustomer.city" />
<button data-ng-click="addCustomer()"> Add Customer </button>
<a href="#/view2"> View 2</a>
</div>
<div cass = "container">
<h2>View </h2>
<div ng-controller="CustomersController">
Search: <input type = "text" ng-model="searchText" />
{{ searchText }}
<br />
<h3> Customers </h3>
<table>
<tr ng-repeat="cust in customers | filter:city">
<td>{{ cust.name }}</td>
<td>{{ cust.city }}</td>
<td>{{ cust.total | currency}} </td>
</tr>
</table>
</div>
You are overriding your app that is making problem
start with var demoApp = angular.module('demoApp', ['helperModule']);
then
again you did declare demoApp
var demoApp = angular.module('demoApp', ['helperModule']);
Better way to solve this issue would be do declare you app only once with all dependency & go on amending it with components like controller, directive, factory, etc.
Code
//could remove `helperModule` module if not used
var demoApp = angular.module('demoApp', ['helperModule', 'ngRoute']);
demoApp.config(function($routeProvider) {
$routeProvider
.when('/', {
controller: "SimpleController",
templateUrl: "views/view1.html"
})
.when('partial2', {
controller: "SimpleController",
templateUrl: "views/view2.html"
})
.otherwise({
redirectTo: "/"
})
});
demoApp.controller("CustomersController", function($scope) {
$scope.customers = [{
name: 'Dave Jones',
city: 'pheonix'
}, {
name: 'Dave Jones',
city: 'New york'
}, {
name: 'Jones suman',
city: 'pheonix'
}, {
name: 'Naresh babu',
city: 'Hyderabad'
}];
$scope.addCustomer = function() {
$scope.customers.push({
name: $scope.newCustomer.name,
city: $scope.newCustomer.city
});
};
});