ng-model到达未定义

时间:2017-12-24 20:45:23

标签: javascript angularjs

ng-model = itemFilter到达未定义但ng-model =第一个div中的项目,如果我得到我想要的。我犯了错误吗?

代码HTML。

<div 
            style="width: 19%; float: left; margin-right: 20px; display: inline-block;">
            <select class="form-control"
                ng-options="item as item.value for item in listItemBook"
                ng-model="item" ng-change="addFilter(item)">
                <option value="">Seleccione una opción</option>
            </select>

        </div>

        <div ng-if="itemBooks"
            style="width: 19%; float: left; margin-right: 20px; display: inline-block;">
            <select class="form-control"
                ng-options="itemFilter as itemFilter.name for itemFilter in listFilter"
                ng-model="itemFilter">
                <option value="" selected>Seleccione una opción</option>
            </select>
        </div>

代码JS。

$scope.search = function() {
        $scope.result = true;   
            console.log($scope.itemFilter);
            console.log($scope.item);
            var delay = $q.defer();
            $http.get('getStatisticList?value='+229+'&typeStatistic='+$scope.item.id+'&startDate='+convertDate($scope.filter.startDate)+'&endDate='+convertDate($scope.filter.endDate))
            .success(function(data, status, headers, config) {              
                delay.resolve(data);
                console.log(data.data)
                $scope.listBooks =data.data; 
            }).error(function(data, status, headers, config) {
                delay.reject('Error new register'); 
            });
    }

1 个答案:

答案 0 :(得分:0)

问题是您直接引用范围中的变量。你应该总是引用范围变量的属性。

function TodoCtrl($scope) {
$scope.listItemBook = [
	{value: "book1"},
	{value: "book2"},
	{value: "book3"},
	{value: "book4"},
];
$scope.listFilter = [
  {name: "old"},
  {name: "new"}
];
$scope.selection = {};
$scope.search = function() {
        $scope.result = true;   
            alert("itemFilter="+JSON.stringify($scope.selection.itemFilter)+", item="+JSON.stringify($scope.selection.item));
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <h2>Todo</h2>
  <div ng-controller="TodoCtrl">
<div 
            style="width: 19%; float: left; margin-right: 20px; display: inline-block;">
            <select class="form-control"
                ng-options="item as item.value for item in listItemBook"
                ng-model="selection.item" ng-change="addFilter(item)">
                <option value="">Seleccione una opción</option>
            </select>

        </div>

        <div ng-if="listItemBook"
            style="width: 19%; float: left; margin-right: 20px; display: inline-block;">
            <select class="form-control"
                ng-options="itemFilter as itemFilter.name for itemFilter in listFilter"
                ng-model="selection.itemFilter">
                <option value="" selected>Seleccione una opción</option>
            </select>
        </div>
        <button ng-click="search();">Search</button>
        </div>
</div>

因此请使用xxxxx.itemFilter而不是itemFilter,并定义此xxxxx。