阅读api和开发指南后,我仍然不明白在给定路由中声明'controller'所提供的功能。现在我只是在我的视图中将我的控制器声明为ng-controller指令。 ngRoute是否只是提供一种替代方法?是否需要角ngRoute'Controller'声明?
要在代码我的问题明确,见下图:
--Index.html
...
<body ng-app="MyApp">
<div ng-view>
</div>
</body>
--View.html
<div id="myView" ng-controller="MyController">
...
</div>
--Route.js
var app = angular.module('MyApp', [ require('angular-route') ]);
app.controller('MyController', ['$scope', function ($scope) {
console.log('this gets executed as I would expect');
}])
.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', { templateUrl: '/Index.html' })
.when('/view', { templateUrl: '/View.html' });
// below line makes no difference as an alternative to above
//.when('/view', { templateUrl: '/View.html', controller: 'MyController' });
}]);
我不知道所有的细节,但至少ng-route还提供了'resolve'功能。 – Rasalom 2014-12-05 10:42:26
当你在路由中指定控制器时,你不需要在视图中指定ng-controller标记。 – 2014-12-05 10:45:26
@ TechMa9iac - 那为什么要这样呢? – aaaaaa 2014-12-05 10:46:01