【发布时间】:2015-06-04 02:01:22
【问题描述】:
我想知道是否有人可以快速浏览一下我的代码,如果我遗漏了什么,请告诉我。我正在尝试学习 Angular.js,并且在 60 分钟的视频中一直在使用著名的 Angular.js。然而,我遇到的问题是 angular 已经更新了,而且事情有点不同。结果,我无法正确编排我的路线。非常感谢任何人提前。
我真正想知道的是,任何人都可以告诉我为了使这些路线正常工作,我缺少什么吗?
以下是我的 html 中的脚本。我跟着这本书,因此它们是我的 index.html 页面中的脚本标签。
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>
这分别是我的视图#1 和视图#2。我觉得一切都是正确的,但是我无法让任何客户的名字出现。
<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>
【问题讨论】:
-
您在查看控制台时是否有任何错误?
-
我猜
controller: "SimpleController"之后缺少的,是复制粘贴问题。此外,$scope.addCustomer不在具有$scope的内部。你能给我们展示一个正在运行的小提琴吗? -
@SergiuParaschiv 我将您的指点包括在我的回答中。谢谢:)
标签: javascript angularjs angularjs-controller angularjs-module