【发布时间】:2015-09-17 08:36:20
【问题描述】:
我创建了一个指令并在同一页面上使用了两次。但是它们被放入另一个指令中
<directive1>
<select-directive/>
</directive1>
<directive1>
<select-directive/>
</directive1>
现在更改其中一个选择的值也应该对另一个选择元素生效。
这可能吗?
编辑
尝试将 $rootScope 与 $broadcast 和 $on 一起使用。但是当从第一个选择字段中选择一个值时,第二个被设置为“空”,就像没有选择任何内容一样。现在从 secong 中选择一些值会导致第一个选择字段为空。
(function() {
'use strict';
var currencySelectionBox = function(ServiceFactory, $rootScope) {
return {
restrict: "E",
replace: true,
scope: false,
template: '<select ng-options="o.currency_code for o in currencyList" ng-model="currencyListSelectedValue" class="form-control"></select>',
link: function(scope, element, attrs) {
ServiceFactory.getCurrencies().then(function(response) {
scope.currencyList = response.data.items;
});
element.bind("change", function(e) {
$rootScope.$broadcast('currencyChanged', scope.currencyListSelectedValue);
});
$rootScope.$on('currencyChanged', function(event, data) {
scope.currencyListSelectedValue = data;
console.log(scope.currencyListSelectedValue);
});
}
};
};
angular
.module('app')
.directive('currencySelectionBox', ['ServiceFactory', '$rootScope', currencySelectionBox]);
})();
【问题讨论】:
标签: angularjs angular-directive