简体   繁体   English

Angular.js-根据同级更改值

[英]Angularjs - Change value based on siblings

I am trying to have multiple dropdown select s which will decide on specific input values 我试图让多个dropdown select将决定特定的输入值

The HTML HTML

<div ng-repeat="i in [1,2,3]">
    <select name="singleSelect" ng-model="chosenitem" style="width:100%;" ng-change="changedValue(i,$index)" >
        <option value="" disabled selected>choose item</option>
        <option ng-repeat="me in stuffs">{{me.item}}</option>
    </select>
    <input type="text" ng-model="inputattr[i]"  placeholder="attribute">
</div>

The controller 控制器

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
  $scope.stuffs = [
    {item:'car', attribute:'wheels'},
    {item:'plane', attribute:'wings'},
    {item:'boat', attribute:'propeller'}
  ];

  $scope.changedValue = function(i,j){
    $scope.inputattr[i] = $scope.stuffs[j].attribute;
  };
});

The problem is inputattr is dynamic. 问题是inputattr是动态的。 I can never get it to work. 我永远都做不到。 Here is my plunk . 这是我的朋克

see this plunkr: http://plnkr.co/edit/GVanAH70oyEEI16Nbf0t 看到这个plunkr: http ://plnkr.co/edit/GVanAH70oyEEI16Nbf0t

you never actually initialized inputattr . 您从未真正初始化过inputattr there also was an issue with your indexes, which should usually start at 0 . 您的索引也有问题,通常应从0开始。

The reason your code is not working is because inputattr is not initialized. 您的代码无法正常工作的原因是未初始化inputattr Add

$scope.inputattr = ['','',''];

inside MainCtrl . MainCtrl内部。 That will do the trick. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM