我有这样的代码:
<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}} <{{item.email}}></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;
};
});
当我输入内容时,我遇到了这个例外,我的代码出了什么问题。
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}} <{{item.email}}></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;
这是CodePen
答案 0 :(得分:4)
简单的错误。添加ng-controller="main"