[英]Load controllers in angular from different files instead just 1
我正在嘗試從不同的js加載控制器,但是我沒有成功,你們能幫我在頁面上加載名為StreetListCtrl.js的控制器嗎?
UsersListCtrl加載完美,只是StreetListCtrl不加載/
我的html:
<!DOCTYPE html>
<html ng-app='myApp' lang="en">
<head>
<meta charset="utf-8">
<title>angularjs</title>
<script src="bower_components/angular/angular.min.js"></script>
<script src='public/javascripts/ng-controller.js'></script>
</head>
<body>
<table ng-controller="UsersListCtrl">
<thead>
<tr>
<th>Username</th>
<th>UserId</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users | orderBy:'id' ">
<td><strong>{{ user.name }}</strong></td>
<td>{{ user.id }}</td>
</tr>
</tbody>
</table>
<p>street list </p>
<table ng-controller="StreetListCtrl">
<thead>
<tr>
<th>street name</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="data in street | orderBy:'id' ">
<td><strong>{{ data.name }}</strong></td>
<td>{{ data.id }}</td>
</tr>
</tbody>
</table>
</body>
</html>
StreetListCtrl.js
angular.module('myApp')
myApp.controller('StreetListCtrl',['$scope',function($scope){
$scope.street =[
{name:'1 smithfield', id:3},
{name:'2 river plate', id:1333},
{name:'3 river plate', id:1}
];
}])
ng-controller.js:
angular.module('myApp', [])
.controller('UsersListCtrl',['$scope',function($scope){
$scope.users =[
{name:'douglas', id:3},
{name:'bruno', id:1}
];
}])
您忘記了第二個javascript文件。 你有:
<head>
<meta charset="utf-8">
<title>angularjs</title>
<script src="bower_components/angular/angular.min.js"></script>
<script src='public/javascripts/ng-controller.js'></script>
</head>
它應該是:
<head>
<meta charset="utf-8">
<title>angularjs</title>
<script src="bower_components/angular/angular.min.js"></script>
<script src='public/javascripts/ng-controller.js'></script>
<script src='public/javascripts/StreetListCtrl.js'></script>
</head>
和StreetListCtrl.js應該是:
angular.module('myApp').controller('StreetListCtrl',['$scope',function($scope){
$scope.street =[
{name:'1 smithfield', id:3},
{name:'2 river plate', id:1333},
{name:'3 river plate', id:1}
];
}])
您缺少index.html中的<script src='public/javascripts/StreetListCtrl.js'></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.