问题
Here when i click on radio button the following div should be disable if the checked radio button is equals to sj.But here its not working if i use ng-repeat for radio button same is working if its static.Could any one help me out what is the mistake i have done here?
<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>
回答1:
Couple of things are missed
- Change you
ng-modelscope variable to point its parent likeng-model="$parent.content"instead ofng-model="content", because ng-repeat creates child scope from current scope. - Use
ng-valueinstead ofvalueattribute for binding scope variable to it.
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>
Working Plunkr
回答2:
you can check the code in this
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>
来源:https://stackoverflow.com/questions/28761516/when-a-radio-button-is-in-ng-repeat-when-i-click-on-radio-button-the-div-will-be