如何从控制器中的多个选项中获取值

时间:2016-05-10 12:04:48

标签: javascript angularjs

我希望从我的结果中选择所有结果,以便在我的控制器中使用。

<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());
    });
});

2 个答案:

答案 0 :(得分:1)

尝试将ng-multiple="true"添加到select

答案 1 :(得分: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.user&#39; s selectedRole属性

中获得多个选定值