当单选按钮处于 ng-repeat 状态时,当我单击单选按钮时,div 将启用?
when a radio button is in ng-repeat when i click on radio button the div will be enable?
在这里,当我点击单选按钮时,如果选中的单选按钮等于 sj.But,则以下 div 应该被禁用,如果我对单选按钮使用 ng-repeat,则它不起作用如果它 static.Could 有人帮我解决我在这里犯的错误是什么?
<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>
遗漏了几件事
- 更改
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>
您可以查看此
中的代码
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>
在这里,当我点击单选按钮时,如果选中的单选按钮等于 sj.But,则以下 div 应该被禁用,如果我对单选按钮使用 ng-repeat,则它不起作用如果它 static.Could 有人帮我解决我在这里犯的错误是什么?
<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>
遗漏了几件事
- 更改
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>
您可以查看此
中的代码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>