无法阅读财产'然后'未定义使用Angular Material自动完成

时间:2016-09-29 08:38:24

标签: javascript angularjs angular-material

我有这样的代码:

<body layout="row" ng-app="myApp" controller="main" ng-cloak>
  <md-autocomplete md-items="item in search(searchText)"
                   md-search-text="searchText"
                   md-item-text="item.name"   
                   md-selected-item="selectedUser"
                   md-no-cache="true"
                   md-floating-label="Enter user ID or email address">
    <md-item-template>
      <span md-highlight-text="searchText">{{item.name}} {{item.surname}} &lt;{{item.email}}&gt;</span>
    </md-autocomplete>
</body>

和js:

var app = angular.module('myApp',['ngMaterial']);
app.controller('main', function($scope, $q, $timeout) {
  var items = [
    {
      userId: 10,
      name: 'Foo',
      surname: 'Bar',
      email: 'email@example.com'
    }
  ];
  $scope.search = function(search) {
    console.log('search');
      var deferred = $q.defer();
      $timeout(function() {
          deferred.resolve(items);
      }, Math.random() * 500, false);
      return deferred.promise;
  };
});

当我输入内容时,我遇到了这个例外,我的代码出了什么问题。

&#13;
&#13;
var app = angular.module('myApp',['ngMaterial']);
app.controller('main', function($scope, $q, $timeout) {
  var items = [
    {
      userId: 10,
      name: 'Foo',
      surname: 'Bar',
      email: 'email@example.com'
    }
  ];
  $scope.search = function(search) {
    console.log('search');
      var deferred = $q.defer();
      $timeout(function() {
          deferred.resolve(items);
      }, Math.random() * 500, false);
      return deferred.promise;
  };
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body layout="row" ng-app="myApp" controller="main" ng-cloak>
  <md-autocomplete md-items="item in search(searchText)"
                   md-search-text="searchText"
                   md-item-text="item.name"   
                   md-selected-item="selectedUser"
                   md-no-cache="true"
                   md-floating-label="Enter user ID or email address">
    <md-item-template>
      <span md-highlight-text="searchText">{{item.name}} {{item.surname}} &lt;{{item.email}}&gt;</span>
    </md-autocomplete>
</body>

<!--
Copyright 2016 Google Inc. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be in foundin the LICENSE file at http://material.angularjs.org/license.
-->
&#13;
&#13;
&#13;

这是CodePen

1 个答案:

答案 0 :(得分:4)

简单的错误。添加ng-controller="main"