我剛開始使用AngularJS。我添加了一個視圖/控制器和路由,它的工作正常。但只要我開始添加另一個路線與不同的控制器,事情不起作用。以下是我的代碼,AngularJS,第二個控制器不工作
<!doctype html>
<html ng-app='simpleApp'>
<body>
<div data-ng-view=""></div>
<script src="js/angular.js"></script>
<script>
var sampleModule = angular.module('simpleApp', []);
var controllers = {};
controllers.SimpleController = function($scope){
$scope.books = [ {title: "The book thief", author:"Unknown"},
{title:"Da Vinci Code", author:"Dan Brown"},
{title:"Gamperaliya", author:"Martin Wickremasinghe"}
];
$scope.addTitle = function()
{
$scope.books.push({title:$scope.newTitle, author:$scope.newAuthor} );
}
$scope.removeTitle = function()
{
$scope.books.pop({title:$scope.newTitle, author:$scope.newAuthor} );
}
}
controllers.detailController = function ($scope)
{
$scope.abc = 'sdsdsd';
$scope.titleDetail = $location.search.title;
alert('dfdfd');
}
sampleModule.controller(controllers);
sampleModule.config(function($routeProvider) {
$routeProvider
.when('/',
{
controller: 'SimpleController',
templateUrl: 'third1.html'
})
.when('/detail/title/:titleId',
{
controller: 'detailController',
templateUrl: 'third2.html'
})
.otherwise({redirectTo: '/' });
});
</script>
的third1.html成功地得到加載,它有一個鏈接加載third2.html我希望提供有關圖書的詳細信息。但在third2.html中我無法獲取在detailController或URL中啓動的值。它不評估{{}}指令。
任何幫助?
的Ish
你不能在同一時間這樣添加多個控制器。您需要爲每個控制器調用'sampleModule.controller('FirstControllerName',controllers.FirstController);''。此外,我建議你閱讀文檔,並遵循下面的例子:http://docs.angularjs.org/guide/controller – jpmorin
@jpmorin:我認爲你可以,那應該工作AFAIK。 – elclanrs
@elclanrs:它會工作,但我不會推薦它(personnally)。 – jpmorin