gpt4 book ai didi

javascript - 无法使用 Angular Material 自动完成读取未定义的属性 'then'

转载 作者:行者123 更新时间:2023-11-28 15:04:24 25 4
gpt4 key购买 nike

我有这样的代码:

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

当我输入一些内容时,我遇到了这个异常,我的代码出了什么问题。

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

这里是CodePen

最佳答案

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

关于javascript - 无法使用 Angular Material 自动完成读取未定义的属性 'then',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com