2016-05-10 44 views
0

我想要從我的結果中選擇所有選擇,以便在我的控制器中使用。如何從控制器中的多個選擇選項中獲取值

<div class="form-group" ng-repeat="att in arr track by $index" ng-hide="check"> 
    <div class="col-md-3"> 
     <label for="phone">{{att.name}}</label> 
    </div> 
    <div class="col-md-9"> 
     <select class="form-control" ng-model="user.charactValue" multiple="true"> 
      <option ng-repeat="itemss in att.value track by $index" value="{{itemss}}" >{{itemss}}</option> 
     </select> 
     <div class="text-center"><a data-toggle="modal" data-target="#addNewCharacteristic" ng-click="getObj(att)">Добави</a></div> 
    </div> 
</div> 
<input type="submit" ng-click="companyBusinessAsset"> 

$scope.companyBusinessAsset = function() { 
    console.log ($scope.charactValue); 
    // this return Undefined 
}; 

向下代碼正在使用,但每次都進入if。

$scope.charactValue = []; 

$scope.$watch ('selected', function(nowSelected) { 
    $scope.selectedValues = []; 
    console.log(nowSelected) 
    // this return Undefined 
    console.log('dddd') 
    if (!nowSelected) { 
     console.log("IFFFFFF"); 
     return; 
    } 
    angular.forEach ($scope.charactValue, function(val) { 
     console.log(val); 
     $scope.charactValue.push (val.id.toString()); 
    }); 
}); 

回答

0

我在下面我的一個項目這樣做,希望這有助於:

<select multiple="multiple" name="selectedRoleId" ng-model="user.selectedRole" class="form-control"> 
     <option ng:repeat="role in roleList" value="{{role.Name}}">{{role.Name}}</option> 
</select> 

然後在控制器提交功能:

//declare $scope.user to avoid undefined/notfound error 
$scope.user = {}; //above the function 
//and inside the function 
$scope.submitForm=function() { 
    var reguser = new models.register($scope.user); 
}; 

你會得到多重選擇的值$scope.userselectedRole屬性

+0

此代碼的工作,但我有3多個選擇選項,在ng-repeat每個選擇具有相同的ng-model.I僅在一個多選選項上獲得結果。 –

+0

然後在瀏覽器中使用firebug/developer explorer檢查你的id的/ ng-models的select元素。他們應該是彼此獨一無二的 –

1

嘗試將ng-multiple="true"添加到您的select

+0

我加入ng-multiple =「true」,但我的選項變成了非標準選項 –

+0

@СтилиянГеоргиевPleas看看這個例子:http://jsfiddle.net/Arg0n/TXPJZ/1607/ – Arg0n

+0

這項工作只有在html中打印結果,但我沒有在我的控制器(未定義)得到velue。 –

相關問題