我以前认为控制器脚本只会在加载单页应用程序时执行一次。
现在我看到每次更改视图时,我的控制器脚本都会再次执行。我可以告诉,因为我在脚本中有几个没有嵌套在函数中的语句。我希望它们仅在首次加载应用时发生,但是当我更改视图时它们正在运行。
使用module.config在我的app.js中配置视图:
myModule.config(function ($routeProvider) {
$routeProvider
.when('/search', {
templateUrl: 'views/search.html',
controller: 'searchCtrl'
})
.when...
那么,当视图发生变化时,控制器脚本是否正常运行?或者我应该搜索我配置错误的内容?
答案 0 :(得分:1)
是的,这是正常的。对我来说它是(/是)相同,但是我使用angular-ui-router ...我选择的解决方案是为了确保控制器中的数据能够持久使用工厂,你可以使用服务或提供者也一样。
"Specialized objects conform to a specific Angular framework API. These objects are one
of controllers, directives, filters or animations.
The injector needs to know how to create these objects. You tell it by registering a
"recipe" for creating your object with the injector. There are five recipe types.
The most verbose, but also the most comprehensive one is a Provider recipe. The
remaining four recipe types — Value, Factory, Service and Constant — are just
syntactic sugar on top of a provider recipe."