阐明angularjs模式中的两个断言

时间:2019-02-05 19:55:36

标签: angularjs

我们经常会发现自己重复相同的事件处理代码,并且 在我们的控制器中,最好将代码移至基本控制器。 然后,我们可以通过使用$ controller服务来添加该功能 基本控制器的属性和功能取决于控制器的作用域

如以下代码所示:

var app = angular.module('angularjs-starter', []);

app.controller('ParentCtrl ', function($scope) {
 // methods to be inherited by all controllers
 // go here
});

app.controller('ChildCtrl', function($scope, $controller) {
 // this call to $controller adds the ParentCtrl's methods
 // and properties to the ChildCtrl's scope
 $controller('ParentCtrl', {$scope: $scope});
 // ChildCtrl's methods and properties go here
});

任何人都可以清楚地告诉我们并给我们提供两个可以阐明的例子 以下两个断言

断言1:

如果我们有多余的事件处理程序需要注销 当视图被破坏时,我们可以确保取消注册事件的代码 通过包含冗余销毁控制器时执行处理程序 基本控制器中的代码,并在以下情况下使用$ on方法执行该代码: $ destroy消息被广播。

断言2:

但是,如果我们使用 通过将ng-controller指令嵌入另一个指令来继承功能 ng-controller指令和用于注销处理程序的代码位于父级中 ng-controller,直到父控制器被执行 已销毁,在这种情况下将不起作用

0 个答案:

没有答案