【发布时间】:2014-02-11 14:46:44
【问题描述】:
当我更改一个选择时,它会触发使用相同指令的其他选择的所有方法绑定。这是预期的行为吗?例如,如果我更改状态选择,它将触发 stateselected 和 dogselected..我是否缺少对 Angular 指令的基本理解?我注意到如果我在没有指令的情况下这样做,这不是行为。
这是我的 plunkr:http://plnkr.co/edit/inKtjY?p=preview
var app = angular.module('app', []);
app.controller('controller', function ($scope) {
$scope.firstName = "Michael";
$scope.states = [{ id: 1, name: 'Nebraska' }, { id: 2, name: 'Iowa' }];
$scope.state = $scope.states[0];
$scope.stateselected = function (state) {
console.log('state selected:', state);
};
$scope.dogs = [{ id: 11, name: 'Poodle' }, { id: 12, name: 'Pitbull' }];
$scope.dog = $scope.dogs[0];
$scope.dogselected = function (dog) {
console.log('dog selected:', dog);
};
});
app.directive('myselect', function () {
return {
restrict: 'E',
template: '<select ng-model="item" ng-options="i as i.name for i in itemlist " ng-selected="itemselected()"></select>',
scope: {
item: '=',
itemlist: '=',
itemselected: '&'
}
};
});
html
<div ng-controller="controller">
State:<myselect item='state' itemlist='states' itemselected='stateselected(state)'></myselect><br>
Dogs: <myselect item='dog' itemlist='dogs' itemselected='dogselected(dog)'></myselect><br>
</div>
【问题讨论】:
标签: angularjs