gpt4 book ai didi

angularjs - Angular 指令 : bind to variable in parent scope

转载 作者:行者123 更新时间:2023-12-03 07:57:42 27 4
gpt4 key购买 nike

Angular 指令演示:

jsfiddle

<div ng-app="myApp">
<script>
function Contrl($scope){
$scope.parval = 0;
$scope.items = [
{id: 1, text: '1'},
{id: 2, text: '2'},
{id: 3, text: '3'}
];
}
</script>
<div ng-controller="Contrl">
A: <input type="radio" value="1" ng-model="parval">1</input>
<input type="radio" value="2" ng-model="parval">2</input>
<input type="radio" value="3" ng-model="parval">3</input>
<item parval="parval" items="items"></item>
</div>
angular.module('myApp', [])
.directive('item', function() {
return {
restrict: 'E',
replace: true,
scope: {
parval: '=',
items: '='
},
template: '<div>' +
'B: <span ng-repeat="i in items">' +
'<input value="{{i.id}}" type="radio" ng-model="parval">{{i.text}}</input>&nbsp;' +
'</span>' +
'</div>'
};
});

现在:
单击 A1 -> B1 选择
单击 A2 -> B2 选择

单击 B1 -> A1 未更改
单击 B2 -> A2 未更改

我想要:
单击 A1 -> B1 选择
单击 A2 -> B2 选择

单击 B1 -> A1 选中
单击 B2 -> 选择 A2

如何?

最佳答案

一种方法是使用 $parent
(ng-model="$parent.parval")

angular.module('myApp', [])
.directive('item', function() {
return {
restrict: 'E',
replace: true,
template: '<div>' +
'B: <span ng-repeat="i in items">' +
'<input value="{{i.id}}" type="radio" ng-model="$parent.parval">{{i.text}}</input>&nbsp;' +
'</span>' +
'</div>'
};
});

关于angularjs - Angular 指令 : bind to variable in parent scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20265797/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com