Angular - 获取动态生成的ng-model值

时间:2017-06-08 12:39:43

标签: angularjs angular-ngmodel

我的问题与此

直接相关

Angular - Dynamic ng-model inside ng-repeater

一切正常,这意味着所有选项都可以正确地进行所有选项。问题在于控制器,我无法获得ng-models的值,它返回“undefined”,我不知道我怎么能这样。

这是我的HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="MyApp" ng-controller="ricercaAttivita">
    <div class="accordion-group" ng-repeat="masterAttribute in masterAttributes">
        <select class="trip dark" ng-model="{{masterAttribute.Name}}" ng-options="attr.Id as attr.Value for attr in masterAttribute.Values">
            <option value="">TUTTE</option>
        </select>
    </div>
    <button class="btn btn-default btn-green btn-3-column sidebar-filtri-button" ng-click="search()">Cerca</button>
</body>

CONTROLLER

var tantoSvagoApp = angular.module('MyApp'); 

tantoSvagoApp.controller("ricercaAttivita") {
  $scope.search = function(page) {
    angular.forEach($scope.masterAttributes,function(value,index){
       console.log($scope.masterAttributes.Name);
    }
  }

1 个答案:

答案 0 :(得分:1)

ng-model删除 {{..}} 。 将其更改为:ng-model="masterAttribute.Name"

并且,在控制器中使用迭代器()而不是foreach中的$scope prop,

喜欢这样:

var tantoSvagoApp = angular.module('MyApp'); 

tantoSvagoApp.controller("ricercaAttivita") {
  $scope.search = function(page) {
    angular.forEach($scope.masterAttributes,function(masterAttribute,index){
       console.log(masterAttribute.Name);
    }
  }
}

同样在linked question中告诉你:)