我有兩個指令,一個是ng-grid,另一個是分頁,當我在一個指令中點擊頁碼時,ng-grid指令應該根據這個改變,我可以有什麼想法在那。如何溝通從一個指令到另一個指令
-1
A
回答
0
有很多方法來實現它: 例如:
首先解決
可以指令之間共享數據:
<directive-one attribute="value" other-attribute="value2" shared-variable="yourData">
<directive-two shared-variable="yourData">
並設置$watch
後第一個指令內值
scope.$watch('yourData',function(newVal,oldVal){ //your logic called after change });
解決方法二
您可以使用事件:
app.directive('first',function(){
return{
restrict: 'E',
template: 'Im first directive!',
scope: true,
link:function(scope,elem,attrs){
scope.$on('event',function(event,args){
alert(args);
});
}
}
});
app.directive('second',function($rootScope){
return{
restrict: 'E',
template: 'Im second directive! <button ng-click="click()">click me!</button>',
scope: true,
link:function(scope,elem,attrs){
scope.click = function(){
$rootScope.$broadcast('event','hello!');
};
}
}
});
事件是由$rootScope.$broadcast('event','hello!');
發送。這意味着事件由您的根範圍向下發送到子範圍。 http://jsfiddle.net/aartek/fJs69/
相關問題
- 1. 如何從另一個指令使用angularjs調用另一個指令的一個指令控制器功能
- 2. 從另一個指令加載一個指令
- 3. 如何將元素高度從一個指令傳遞給另一個指令?
- 4. 指令可與其他指令溝通
- 5. IONIC:將數據從一個指令傳遞到另一個指令
- 6. 是否可以用另一個指令代替一個指令
- 7. 如何編寫生成另一個指令的AngularJS指令?
- 8. 指令另一個指令裏面angularjs而內部指令是從另一個模塊
- 9. 從另一個指令角添加fxFlex指令
- 10. 從另一個指令訪問指令範圍變量
- 11. 從angularjs中的另一個指令內添加指令
- 12. 如何基於另一個指令邏輯創建一個AngularJS指令?
- 13. 如何獲得一個角度指令來包含&替換另一個指令?
- 14. UI屏蔽指令添加到另一個指令
- 15. 一個指令
- 16. AngularJs與指令溝通
- 17. 按指定範圍在另一個指令中插入指令
- 18. 如何綁定到另一個屬性從結構指令中
- 19. LLVM - 如何得到一個指令
- 20. 另一個指令模板中的指令
- 21. 在另一個指令中嵌入AngularJS指令
- 22. 使用按鈕調用另一個指令內部的指令?
- 23. AngularJS:在我的指令中應用另一個指令
- 24. 在另一個指令的鏈接功能中添加指令
- 25. 測試依賴於另一個指令的指令 - Angularjs
- 26. AngularJS:指令導致另一個指令激活,導致錯誤
- 27. 在指令內編譯另一個指令不起作用
- 28. 調用另一個指令的模板指令
- 29. 動態地在另一個指令中包含指令
- 30. 自定義HTML自動換行指令(另一個指令)
你能爲你的問題添加一些代碼嗎? – maurycy
根據您的具體情況,使用'$ scope。$ broadcast','$ scope。$ emit'或'$ rootScope。$ broadcast'。 https://docs.angularjs.org/api/ng/type/$rootScope.Scope – domakas