这里当我点击单选按钮时,如果选中的单选按钮等于sj,则应禁用以下div。但是,如果我使用ng-repeat作为单选按钮,则它不起作用如果它的静态可以正常工作。可以任何一个帮助我在这里做了什么错误?
<div ng-app="plunker">
<div class="wrap" ng-controller="MyCtrl">
<h1>Hello there!</h1>
<p>Push the radio buttons to change the content!</p>
<form>
<div ng-repeat="s in color">
<input id="first" type="radio" name="content" ng-model="content" value="s.name">{{s.name}}
<br/></div>
</form>
<div class="wrapper">
<p ng-show="content == 'sj'">This is the first content!</p>
</div>
</div>
</div>
<script>
var app = angular.module('plunker', []);
app.controller('MyCtrl', function($scope) {
$scope.color = [{"name":"sj"},{"name":"asfdsaf"},{"name":"sdfsadf"},{"name":"sdf"}];
});
</script>
答案 0 :(得分:2)
错过了一些事情
ng-model
范围变量更改为指向其父级,而不是ng-model="$parent.content"
,而不是ng-model="content"
,因为ng-repeat会从当前范围创建子范围。ng-value
代替value
属性来绑定范围变量。<强> HTML 强>
<form>
<div ng-repeat="s in color">
<input id="first" type="radio" name="content" ng-model="$parent.content" ng-value="s.name">{{s.name}}
<br/>
</div>
</form>
答案 1 :(得分:1)
您可以查看此
中的代码
var app = angular.module('plunker', []);
app.controller('MyCtrl', function ($scope) {
$scope.contentshow=true;
$scope.color = [{
"name": "sj"
}, {
"name": "asfdsaf"
}, {
"name": "sdfsadf"
}, {
"name": "sdf"
}];
$scope.showsj=function(radioshow){
if(radioshow.name==='sj'){
$scope.contentshow=false;
}
else{
$scope.contentshow=true;
}
};
})
.done-true {
text-decoration: line-through;
color: grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js"></script>
<div ng-app="plunker">
<div class="wrap" ng-controller="MyCtrl">
<h1>Hello there!</h1>
<p>Push the radio buttons to change the content!</p>
<form>
<div ng-repeat="s in color">
<input id="first" type="radio" name="content" ng-model="content" value="s.name" ng-click="showsj(s)">{{s.name}}
<br/>
</div>
</form>
<div class="wrapper">
<p ng-show="contentshow">This is the first content!</p>
</div>
</div>
</div>