0
我的要求是從父指令調用子指令函數。目前我們已經實現了發佈 - 訂閱模式。 Child指令訂閱某個事件,並且我們對父指令進行觸發。 發佈訂閱是通過$ .callbacks實現的。角度雙向函數綁定最佳實踐
這個可以做的其他方法是使用廣播。或者一個子指令監視某個變量,我們可以在父變量上更改此變量。
我們沒有使用廣播,因爲父指令包含大量的子事件,我們不希望它全部通過。
有我們這是用雙向功能結合
directives.directive('twoWayBind', function() {
return {
restrict: "E",
transclude: true,
templateUrl: 'app/twoWayBindFunction.html',
scope: {
twoWayBinding: '='
},
controller: 'twoWayBindFunctionController',
bindAsController: true,
controllerAs: 'vm',
link: function (scope, element, attrs, controller) {
}
};
});
controllers.controller('twoWayBindFunctionController', ['$scope', function (scope) {
var vm = this;
vm.scope = scope;
scope.twoWayBinding = function() {
console.log('twoway bind');
}
}]);
我們可以從父範圍調用twoWayBinding功能實現的另一種方式。
我想了解什麼是最佳實踐。