Bootstrap下拉和AngularJs相同的ng-model用于ng-repeat内的下拉列表

时间:2017-03-22 18:32:43

标签: angularjs drop-down-menu angular-ngmodel angularjs-ng-change ng-controller

我有规则列表,在每个规则中都有属性属性,应该从属性下拉列表中选择它。在ng-repet中,我显示所有规则,即每个属性都具有相同的ng-model,ng-controller和ng-change。这可能会混淆应用程序。我不知道如何在下拉列表中设置attrbiute,这是从ruleList [i] .attribute中获取的。从下拉列表中进行选择时,会选择正确的selectedAttribute - ng-change。当想要从下拉列表中设置值时,它不起作用。如何添加一些索引或什么? 我已经按照这个例子写了它:http://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/06/29/how-to-work-with-the-bootstrap-dropdown-in-angularjs.aspx

HTML code:

<tr ng-repeat="r in vm.ruleset.ruleList track by $index" ng-init="$ruleIndex = $index">
    <td align='right'>
        <div ng-controller="vm.attributeDropdown" init-data="$ruleIndex" class="col-xs-12 col-sm-4 col-md-3">
            <div class="form-group" ng-class="{ 'has-error': vm.rulesetForm.attribute.$touched && vm.rulesetForm.attribute.$invalid }">
                <div>
                    <span>{{selectedAttribute}}</span>
                        <select class="form-control" required ng-model="selectedAttribute" ng-options="i.attributenamecap for i in vm.attributes" id="vm.rulesetForm.attribute" ng-change="correctAttribute($ruleIndex, selectedAttribute)">
                            <option style="display:none" translate="rulesets.ruleset.form.placeholder.attribute""></option>
                        </select>
                        </div>
                       <div class="help-block" ng-show="vm.rulesetForm.$submitted || vm.rulesetForm.attribute.$touched">
                       <span ng-show="vm.rulesetForm.attribute.$error.required" translate=  "rulesets.ruleset.form.error.required.attribute"></span>
                </div>
            </div>
        </div>
    </td>
</tr>

关于ng-change的控制器:

 $scope.correctAttribute = function(index, selectedAttribute){
    if(index !== undefined || index !== null){
        vm.ruleset.ruleList[index].attribute = selectedAttribute;
    }
}

ng-controller上的控制器:

function attributeDropdown($scope){
    var index = $scope.$ruleIndex;
    $scope.vm.attributes = vm.attributes;
    $scope.vm.attributes.unshift('');
    if(!isNew()){
        if(vm.ruleset.ruleList[index].attribute === ''){
            vm.ruleset.ruleList[index].attribute = null;
            $scope.selectedAttribute = vm.ruleset.ruleList[index].attribute;
            vm.original = angular.copy(vm.ruleset);
        }else{
            $scope.selectedAttribute = vm.ruleset.ruleList[index].attribute;
        }
    }else if(vm.ruleset.ruleList[index].attribute !== undefined ){
    $scope.selectedAttribute = vm.ruleset.ruleList[index].attribute;
    }
}

1 个答案:

答案 0 :(得分:1)

使用ng-model="r.selectedAttribute"代替ng-model="selectedAttribute"

类似这样的事情

 <select class="form-control" required ng-model="r.selectedAttribute" ng-options="i.attributenamecap for i in vm.attributes" id="vm.rulesetForm.attribute" ng-change="correctAttribute($ruleIndex, r.selectedAttribute)">
         <option style="display:none" translate="rulesets.ruleset.form.placeholder.attribute""></option>
    </select>