Angular JS指令加载解释

时间:2014-09-20 21:59:25

标签: angularjs angularjs-directive angularjs-scope

我希望了解页面加载一次然后返回使用路由时角度指令的预期行为。如果我在页面上有下面的指令,则在首次加载页面时会到达debugger行。当我从该页面导航到另一个具有不同控制器的页面时,然后返回带有该指令的原始页面,该指令不会加载。这是处理不同控制器时的预期行为吗?或者每次加载页面时都应该调用指令link吗?

app.directive('directive1', function () {
return {
    restrict: 'E',
    replace: true,
    template: '<div></div>',
    link: function (scope, element, attr) {
        debugger;
        console.log('directive loaded');
    }
   }
});

它就在这样的页面上

<directive1 id="mydirective" style="height:100%;"></directive1>

流程就像这样

pg1.htm(directive)/controller1 --> pg2.htm/controller2 --> back to pg1.htm(directive)/controller1

1 个答案:

答案 0 :(得分:1)

由于这是SPA,您正在AngularJS App中移动,每次都不会加载该指令。但是一旦刷新页面就会被加载。