在Angular中使用ng-repeat时更改输入参数

时间:2014-07-24 03:51:37

标签: angularjs angularjs-ng-repeat

我需要为ng-repeat指令显示的一个单独输入更改ng-Disabled值。

我有:

<li ng-repeat="lang in langs">
    <label> <input 
             type="radio" 
             name="language"
             ng-disabled="isDisabled"
             ng-model="langs.name"
             ng-value="$index"
             ng-change="getIndex($index)"
             />
     <span> {{lang.name}} </span> 
     </label>    
</li>

如何做这样的事情:

$scope.langs[0].ngDisabled = true;

感谢。

1 个答案:

答案 0 :(得分:0)

您可以允许某个属性“禁用”&#39;要添加到您的模型中:

<li ng-repeat="lang in langs">
    <label> <input 
         type="radio" 
         name="language"
         ng-disabled="lang.isDisabled"
         ng-model="lang.name"
         ng-value="$index"
         ng-change="getIndex($index)"
         />
     <span> {{lang.name}} </span> 
     </label>    
</li>

然后你可以这样做:

  $scope.langs[0].isDisabled = true;

或者,如果您不想改变模型,可以改为:

控制器:

var app = angular.module("app", []);
app.controller('Ctrl', function ($scope) {
   $scope.isDisabled = {};
   $scope.langs = [
      { name: 'French' },
      { name: 'English' },
      { name: 'German' },
      { name: 'Chinese' },
      { name: 'Dutch' }
   ];


});

HTML

  <body ng-app="app" ng-controller='Ctrl as ctrl'> 
      <ul>
           <li ng-repeat="lang in langs">
              <label> 
                <input 
                  type="radio" 
                  name="language"
                  ng-disabled="isDisabled[lang.name]"
                  ng-model="ctrl.selectedLanguage"
                  ng-value="lang.name"
                  />
                <span> {{lang.name}} </span> 
             </label>    
          </li>          
      </ul>
     <br />
     {{ ctrl.selectedLanguage }} <br />
     {{ isDisabled }}
 </body>

然后你可以这样做:

   $scope.isDisabled[$scope.langs[0].name] = true;

Plunker Demo