when a radio button is in ng-repeat when i click on radio button the div will be enable?

旧时模样 提交于 2019-12-11 11:10:53

问题


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

  1. Change you ng-model scope variable to point its parent like ng-model="$parent.content" instead of ng-model="content", because ng-repeat creates child scope from current scope.
  2. Use ng-value instead of value attribute 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!