当单击单选按钮时单选按钮处于重复按钮时,div将启用吗?

时间:2015-02-27 09:26:35

标签: angularjs angularjs-scope angularjs-ng-repeat

这里当我点击单选按钮时,如果选中的单选按钮等于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>

2 个答案:

答案 0 :(得分:2)

错过了一些事情

  1. 将您的ng-model范围变量更改为指向其父级,而不是ng-model="$parent.content",而不是ng-model="content",因为ng-repeat会从当前范围创建子范围。
  2. 使用ng-value代替value属性来绑定范围变量。
  3. <强> 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

答案 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>