我一直在编写本教程“AngularJSDemos”Dan Wahlin链接https://www.youtube.com/watch?v=i9MHigUZKEM,但应用程序SPA AngularJS未在网页上呈现。我的代码中是否有任何遗漏?
Html页面包含以下代码:“使用AngularJS指令和数据绑定”
<!DOCTYPE html>
<html data-ng-app="demoApp">
<head>
<title>Using AngularJS Directives and Data Binding</title>
</head>
<body>
<div>
<!--Placeholder for views-->
<div data-ng-view=""></div>
</div>
<script src="Scripts/angular.min.js"></script>
<script>
var demoApp = angular.module('demoApp', []);
demoApp.config(function ($routeProvider) {
$routeProvider
.when('/view1',
{
controller: 'SimpleController',
templateUrl: 'Partials/View1.html'
})
.when('/view2',
{
controller: 'SimpleController',
templateUrl: 'Partials/View2.html'
})
.otherwise({ redirectTo: '/view1' });
});
demoApp.factory('simpleFactory', function () {
var customers = [
{ name: 'John Smith', city: 'Phoenix' },
{ name: 'Jane Doe', city: 'San Francisco' },
{ name: 'John Doe', city: 'New York' },
{ name: 'Mike Leski', city: 'Clarksburg' },
];
var factory = {};
factory.getCustomers = function() {
return customers;
};
factory.postCustomer = function (customer) {
};
return factory;
});
demoApp.controller('SimpleController', function ($scope, simpleFactory) {
$scope.customers = [];
init();
function init() {
$scope.addCustomer = simpleFactory.getCustomers();
}
$scope.addCustomer = function () {
$scope.customers.push(
{
name: $scope.newCustomer.name,
city: $scope.newCustomer.city
});
};
});
</script>
View1.html
<div class="container">
<h2>View 1</h2>
Name:
<br />
<input type="text" data-ng-model="filter.name" />
<br />
<ul>
<li data-ng-repeat="cust in customers | filter:filter.name | orderBy:'city'">{{ cust.name | uppercase}} - {{ cust.city | lowercase}}</li>
</ul>
<br />
Customer Name:<br />
<input type="text" data-ng-model="newCustomer.name"/>
<br />
Customer City:<br />
<input type="text" data-ng-model="newCustomer.city" />
<br />
<button data-ng-click="addCustomer()">Add Customer</button>
<br />
<a href="#/view2">View 2</a>
</div>
最后一个View2.html
<div class="container">
<h2>View 2</h2>
City:
<br />
<input type="text" data-ng-model="city" />
<br />
<ul>
<li data-ng-repeat="cust in customers | filter:city | orderBy:'city'">{{ cust.name | uppercase}} - {{ cust.city | lowercase}}</li>
</ul>
</div>
答案 0 :(得分:1)
问题是您在声明demoApp模块时缺少ngRoute
依赖项:
var demoApp = angular.module('demoApp', ['ngRoute']);
Here是您代码的工作示例
还记得添加ngRoute的脚本,你可以使用:
npm install angular-route --save-dev
或者添加CDN脚本:
例如https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.js
PD:我建议您使用ui-router更强大