我是Angular.js的新手,我的应用程序需要指令之间的某些通信,我阅读了一些有关链接和需求的文档,但无法确切了解其工作原理。
对于一个简单的例子,我有:live小提琴:http : //jsfiddle.net/yw235n98/5/
2个指令:firstDir,secondDir ::带有一些数据
firstDir具有单击功能,它将更改数据值
当firsDir单击功能被触发时,我也想在secondDir中更改数据。
HTML:
<body ng-app="myApp">
First Directive :
<first-dir >
<h3>{{firstCtrl.data}}</h3>
<button ng-click="firstCtrl.set('NEW VALUE')">Change Value</button>
</first-dir>
Second Directive :
<second-dir>
<h3>{{secondCtrl.data}}</h3>
</second-dir>
Javascript:
(function(){
var app = angular.module('myApp', []);
app.directive("firstDir", function(){
return {
restrict : 'E',
controller : function(){
this.data = 'init value';
this.set = function(value){
this.data = value;
// communication with second Directive ???
}
},
controllerAs : 'firstCtrl'
};
});
app.directive("secondDir", function(){
return {
restrict : 'E',
controller : function(){
this.data = 'init value';
},
controllerAs : 'secondCtrl'
};
});
})();
弑天下
饮歌长啸
相关分类