简体   繁体   English

md-autocomplete 返回 TypeError:无法读取未定义的属性“then”

[英]md-autocomplete returns TypeError: Cannot read property 'then' of undefined

Using the md-autocomplete component from angular material I've got a problem:使用angular materialmd-autocomplete组件我遇到了一个问题:

<md-autocomplete
        required
        md-search-text="searchTxt"
        md-selected-item-change="setModelValue(item.name)"
        ng-model="searchTxt"
        md-search-text-change = "searchItem(searchTxt)"
        md-items="item in pickerResult"
        md-item-text="item.name"
        md-min-length="0"
        md-delay="100"
        placeholder="Search...">
    <md-item-template>
        <span md-highlight-text="searchTxt" md-highlight-flags="^i">{{item.title}}</span>
    </md-item-template>
    <md-not-found>
       No results <span data-ng-if="form.detailModel.aspectName != null">per</span> {{form.detailModel.aspectName}}
    </md-not-found>
</md-autocomplete>

this is the function in my controller这是我的控制器中的功能

$scope.searchAspect = function(searchStr) {
    if(!searchStr) {
        var searchStrEncoded = "";
    } else {
        var searchStrEncoded = escape(searchStr);
    }
    var url = "/api/url&searchTxt=" + searchStrEncoded;
    $http({
        url: url,
        method: 'GET'
    }).success(function (data, status, headers, config) {
        $scope.pickerResult = data.data;
    });
};

If I type something I get the data.如果我输入一些东西,我就会得到数据。 but on blur in the input I get this error: TypeError: Cannot read property 'then' of undefined and I can't get my data back.但是在输入中模糊时,我收到此错误: TypeError: Cannot read property 'then' of undefined我无法取回我的数据。 I tried to change the md-items directive in this way我试图以这种方式更改 md-items 指令

md-items="item in searchItem(searchTxt)"

and I didn't get the error but the autocomplete shows no results even if the http call was successful.我没有收到错误,但即使 http 调用成功,自动完成也没有显示任何结果。 Any ideas?有什么想法吗?

EDIT with the promise编辑承诺

$scope.searchAspect = function(searchStr) {
        if(!searchStr) {
            var searchStrEncoded = "";
        } else {
            var searchStrEncoded = escape(searchStr);
        }
        var deferred = $q.defer();
        var url = "/api/url&searchTxt=" + searchStrEncoded;
        $http({
            url: url,
            method: 'GET'
        }).success(function (data, status, headers, config) {
            deferred.resolve(data.data);
            $scope.pickerResult = data.data;
        }).error(deferred.reject);
        return deferred.promise;
    };

same error同样的错误

try this试试这个

<md-autocomplete
        required
        md-search-text="searchTxt.val"
        md-items="item in searchAspect(searchTxt)"
        md-item-text="item.name"
        md-min-length="0"
        md-delay="100"
        placeholder="Search...">
    <md-item-template>
        <span md-highlight-text="searchTxt" md-highlight-flags="^i">{{item.title}}</span>
    </md-item-template>
    <md-not-found>
       No results <span data-ng-if="form.detailModel.aspectName != null">per</span> {{form.detailModel.aspectName}}
    </md-not-found>
</md-autocomplete>

$scope.searchAspect = function(searchStr) {
    if(!searchStr.val) {
        var searchStrEncoded = "";
    } else {
        var searchStrEncoded = escape(searchStr);
    }
    var url = "/api/url&searchTxt=" + searchStrEncoded;
    return $http({
        url: url,
        method: 'GET'
    }).then(function (data) {
        return data.data;
    });
};

as i understand md-items attributes needed a promise, and you are providing a array.据我了解,md-items 属性需要一个承诺,而您提供的是一个数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有错误的md-autocomplete:无法读取未定义的属性“成功” - md-autocomplete with error: Cannot read property 'success' of undefined 类型错误:无法读取 Angular 2 中未定义的属性“自动完成” - TypeError: Cannot read property 'Autocomplete' of undefined in Angular 2 JQuery 自动完成:未捕获的类型错误:无法读取未定义的属性“值” - JQuery autocomplete: Uncaught TypeError: Cannot read property 'value' of undefined 未捕获的TypeError:无法读取未定义的JQUERY自动完成的属性&#39;length&#39; - Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete md-autocomplete未显示 - md-autocomplete not displayed toDataURL返回错误“未捕获的TypeError:无法读取未定义的属性&#39;0&#39;” - toDataURL returns error “Uncaught TypeError: Cannot read property '0' of undefined ” 先拼接 object 返回 TypeError: Cannot read property of undefined - Splicing first object returns TypeError: Cannot read property of undefined TypeError:无法读取未定义的属性“0” - TypeError: Cannot read property '0' of undefined TypeError无法读取未定义的属性 - TypeError Cannot read property of undefined TypeError:无法读取未定义的属性… - TypeError: Cannot read property … of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM