Angularjs 指令中的Select,TRANCLUDE不更改父范围
小提琴: 下面的代码不起作用。 如果我删除了transclude和模板,它将按预期工作。 我错过了什么Angularjs 指令中的Select,TRANCLUDE不更改父范围,angularjs,angularjs-directive,angularjs-scope,Angularjs,Angularjs Directive,Angularjs Scope,小提琴: 下面的代码不起作用。 如果我删除了transclude和模板,它将按预期工作。 我错过了什么 <div ng-app="example"> <div ng-controller="TodoCtrl"> <div ng-repeat="car in carsOwned"> <h3>Owned car: {{car.brand}}</h3> <
<div ng-app="example">
<div ng-controller="TodoCtrl">
<div ng-repeat="car in carsOwned">
<h3>Owned car: {{car.brand}}</h3>
<div example-select ng-model="car">
<select ng-model="car" ng-options="carType.brand for carType in carTypes"></select>
<p>Why doesn't the select alter the owned car?</p>
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.carTypes = [{
brand: 'BMW',
value: 'bmw'
}, {
brand: 'Mercedes',
value: 'me'
}, {
brand: 'Fiat',
value: 'fi'
}];
$scope.carsOwned = [$scope.carTypes[0]];
}
angular.module('example', []).directive('exampleSelect', function () {
return {
transclude: true,
scope: {
car: '='
},
link: function (scope, elm, attrs, ctrl) {
console.log('alive');
},
template: '<div ng-transclude></div>'
};
});
自有汽车:{{car.brand}
为什么选择不改变拥有的汽车
函数TodoCtrl($scope){
$scope.carTypes=[{
品牌:“宝马”,
价值:“宝马”
}, {
品牌:“梅赛德斯”,
价值观:“我”
}, {
品牌:“菲亚特”,
值:“fi”
}];
$scope.carsOwned=[$scope.carTypes[0]];
}
angular.module('example',[])指令('exampleSelect',函数(){
返回{
是的,
范围:{
汽车:'='
},
链接:函数(范围、elm、属性、ctrl){
console.log('alive');
},
模板:“”
};
});
ng transclude
创建子作用域,因此需要设置对象成员才能访问父作用域变量
编辑 为了避免在选择选项中放置空选项,还需要根据设置初始值
自有汽车:{{car.value.brand}
ng transclude
创建子作用域,因此需要设置对象成员才能访问父作用域变量
编辑 为了避免在选择选项中放置空选项,还需要根据设置初始值
自有汽车:{{car.value.brand}
绑定到
carsOwned[$index]
而不是car
工作:
<select ng-model="carsOwned[$index]" ng-options="carType.brand for carType in carTypes"></select>
以下是对您的提琴的更新:绑定到
carsOwned[$index]
而不是car
工作:
<select ng-model="carsOwned[$index]" ng-options="carType.brand for carType in carTypes"></select>
以下是您的小提琴的更新:
<div ng-repeat="car in carsOwned track by $index">