我完成了 tutorial on the AngularJS website我注意到在step 7 ,它们改变了将 Controller 引入应用程序的方式。最初,他们使用指令:
<body ng-controller="PhoneListCtrl">
...
</body>
但是,它后来被更改为使用 controller
属性作为 ng-route
的一部分。
$routeProvider.
when('/phones', {
templateUrl: 'partials/phone-list.html',
controller: 'PhoneListCtrl'
}).
/* rest of routes here */
这是 git diff进行更改的位置。这两种技术之间有区别吗?
最佳答案
使用 ng-controller 指令的 Controller :
- 在
ng-controller
元素上创建了一个新的 $scope。 - 显式 View 到 Controller 连接
- 通过检查元素等可见
路由中的 Controller :
- 在
ng-view
元素上为每个路由创建一个新的 $scope。 - Controller 可以请求路由解析中定义的依赖项。
- 可选的 View 到 Controller 连接。建议有一个命名约定,将路由映射到 Controller 到 View 。
关于javascript - ng-controller 指令和路由中的 Controller 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21433879/