AngularJS控制器执行的每个视图路由时间 [英] AngularJS controller is executed every time the view is routed

查看:93
本文介绍了AngularJS控制器执行的每个视图路由时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每次更改通过链接路径类似以下

Every time I change the path through a link like the following

<li><a href="#/home">Home</a></li>

在路由器定义视图控制器被再次运行。

The controller for the view in the router definition gets run again.

config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        // $locationProvider.hashPrefix('!');

        $routeProvider.when('/home', {
            templateUrl: 'partials/home.html',
            controller: 'mainCtrl'
        });
        $routeProvider.when('/test', {
            templateUrl: 'partials/test.html',
            controller: 'testCtrl'
        });
        $routeProvider.otherwise({
            redirectTo: '/home'
        });
    }
]);

我不认为这是默认的行为(我发现的文件中没有提到它),但是我看不出有什么问题。

I don't think that this is default behavior (I found no mention of it in the documentation), however I can't see what the problem is.

P.S。

我没有在我的模板分配到任何DOM元素的NG-控制器,因为我看过别人用类似的问题,即这是问题。

I don't have an ng-controller assigned to any DOM element in my templates since I've seen someone else with a similar issue where this was the problem.

推荐答案

这是一个默认行为。

基本上,控制器是用来论证角度范围的功能。因此需要每个与控制器相关联的网页被打开时被调用。每次您的网页导航角时将创造新的范围。

It is a default behavior.
Basically controller is function used to argument Angular Scope. So it need to be called each time the page associated with the controller is opened. Each time your page is navigated angular will create new scope.

这篇关于AngularJS控制器执行的每个视图路由时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆