角指令中link和controller作用域共享的混淆
Confusion on scope sharing in angular directive between link and controller
我理解下面的代码有点困难
HTML <div my-example max="77"></div>
指令 angular
.module('app')
.directive('myExample', myExample);
function myExample() {
var directive = {
restrict: 'EA',
templateUrl: 'app/feature/example.directive.html',
scope: {
max: '='
},
link: linkFunc,
controller: ExampleController,
controllerAs: 'vm',
bindToController: true // because the scope is isolated
};
return directive;
function linkFunc(scope, el, attr, ctrl) {
console.log('LINK: scope.min = %s *** should be undefined', scope.min);
console.log('LINK: scope.max = %s *** should be undefined', scope.max);
console.log('LINK: scope.vm.min = %s', scope.vm.min);
console.log('LINK: scope.vm.max = %s', scope.vm.max);
}
}
ExampleController.$inject = ['$scope'];
function ExampleController($scope) {
// Injecting $scope just for comparison
var vm = this;
vm.min = 3;
console.log('CTRL: $scope.vm.min = %s', $scope.vm.min);
console.log('CTRL: $scope.vm.max = %s', $scope.vm.max);
console.log('CTRL: vm.min = %s', vm.min);
console.log('CTRL: vm.max = %s', vm.max);
}
HTML <!-- example.directive.html -->
<div>hello world</div>
<div>max={{vm.max}}<input ng-model="vm.max"/></div>
<div>min={{vm.min}}<input ng-model="vm.min"/></div>
为什么scope.vm.min
定义在linkFunc
中?link函数是否共享控制器的作用域?尽管如此,为什么vm
不像$scope.vm = vm
那样进行显式赋值就自动添加到作用域?
vm存储在控制器的$作用域中。使用这个in指令,我们使用scope而不是$scope。因此,在这种情况下,由于'vm'是控制器的$作用域,它与指令的作用域相同。链接通过在linkFunc的参数中传递作用域来使用它。当指令使用控制器时,同样的作用域被传递给link函数。因此,我们可以直接使用scope.vm.min和其他任何东西。我希望你能理解。
强烈建议你学习指令的基础知识http://www.sitepoint.com/practical-guide-angularjs-directives/
vm什么也不是但是你必须使用controlleras
语法所以它们的意思是
作为控制器
由于指令配置中的controllerAs
语法,vm
会自动添加到作用域。
return {
controller: Controller
controllerAs: 'vm'
};
controllerAs
语法创建控制器函数的实例,this
对象上的所有内容将通过controllerAs
值中给定的别名暴露在作用域中。vm
存在于您的link
函数中,因为在您的控制器和您的链接函数中,您的指令具有相同的$scope
。它不是"共享的",但它是相同的。当编译指令时,它会接收到一个新的$作用域。在编译时,控制器出现并将一些信息附加到作用域。当指令完全呈现时,link
函数启动,并在函数的参数中提供相同的$scope
。这就是为什么您可以访问link
函数中的vm
。不管你在视图中声明了什么,比如ng-model=vm.min
,它也会附在作用域的vm
上。
Angular编译你的指令时所发生的事情的一个更直观的表示(大概):
// First step preparing the new scope and the directive configuration
var $newScope = $rootScope.$new(true); // true for isolated scope
var directiveConfig = {
controller: Controller,
controllerAs: 'vm',
link: function(scope, elem) { ... }
}
// Attaching the new controller instante to your new scope
$newScope[directiveConfig.controllerAs] = new directiveConfig.controller();
// At this point, we have the scope.vm
//Compile the directive to recieve the rendered element attach to the DOM
var elem = $compile('<my-directive></my-directive')($newScope);
// At this point, our scope.vm now contains scope.vm min and scope.vm.max because of the ng-model
// Call the link function with the newScope and the newly renrered element
directiveConfig.link.call(null, $newScope, elem);
- 在Angular Bootstrap Modal和父控制器之间共享作用域
- AngularJS:如何与其他控制器共享作用域函数和变量
- AngularJS:与同级或后代作用域共享作用域行为
- 在控制器之间共享作用域变量的最佳实践是什么
- 在Node中与jsdom和jquery共享变量作用域
- 在指令作用域和控制器$scope之间共享数据
- 多个指令需要共享同一个私有作用域
- 正在与子指令作用域共享作用域对象
- AngularJS-在控制器之间共享作用域值
- 如何在不同的JS文件之间共享相同的作用域变量
- Angular中两个不同的指令在同一个控制器上共享作用域
- 如何在angularjs中使用两个不同的ui路由器在状态之间共享作用域数据?
- AngularJS:在两个具有隔离作用域的指令之间共享一个控制器作用域
- 角指令中link和controller作用域共享的混淆
- Angularjs:拥有相同对象数据的2个不同作用域共享相同行为
- 多个指令共享同一个作用域
- 可以共享作用域和访问实例的私有原型方法
- Angularjs:两个视图,同一个控制器——共享作用域
- 变量在HTTP $作用域中不可共享
- AngularJS中的父控制器'作用域vs共享服务