1
  • 角 1.5.8
  • 引导程序 3.3.7 (CSS)
  • Angular-ui 2.0.2

使用 angular Typeahead ( ui.bootstrap.typeahead ) 需要一个对象列表,它将在带有 HTML 的 ui 组件中显示

问题

  1. 从服务返回promise到组件(1.5 样式控制器、视图、绑定

  2. 控制器函数使用promise从服务返回并执行then逻辑并返回array对象

  3. Typeahead 不处理数组...执行控制台日志,您可以看到数组。

  4. 如果我在不使用服务方法的情况下静态传递相同的对象数组,那么该功能将起作用

HTML

            <input type="text" ng-model="$ctrl.search.term" ng-disabled="!$ctrl.search.type"
                   typeahead-wait-ms="600"
                   placeholder="Search..."
                   uib-typeahead="res as res.name for res in $ctrl.submit($viewValue)"
                   typeahead-no-results="noResults" class="form-control" required>
            <i ng-show="loadingLocations" class="icon ion-refresh"></i>

            <div ng-show="noResults">
                <i class="icon ion-close"></i> No Results Found
            </div> 

            <select class="form-control custom-select-md"
                   ng-model="$ctrl.search.type"
                   placeholder="Type"
                   required>
                <option value="" disabled selected>Select Type?</option>
                <option value="car">car</option>
                <option value="van">van</option>
            </select>

组件(控制器、视图)

//submit search for issuers or issuedCard
submit() {
    this.isSubmitting = true;

    this._SearchService.performSearch(this.search)
    .then(
        (resp) => {
            //e.g. [{id:1, name:'test'}]
            console.log('Search Result', resp);
            return resp;                                
        },

        (err) => {
            console.log('Error Search', err);
            this.reset(false);
            this.errors = err;
            return [];
        }
    );

    //Comment out method above to see this static data returning and working as should be :'(
    //return [{id:865,issuer: {},name:"British Testing"},
    //    {id:866,issuer: {},name:"American Testing"}];
}

服务

performSearch(searchData) {
    console.log('Search Qry', searchData);

    let deferred = this._$q.defer();

    if(!this.isValidSearch(searchData)) {
        deferred.reject({status:400,  error: 'Bad Request', message:'invalid data'});
        return deferred.promise;
    }

    let searchURI = (searchData.type === 'car' ? 'van' : 'issuer');

    this._$http({
        url: `${this._AppConstants.api}/${this._AppConstants[searchURI]['search']}`,
        method: 'GET',
        params: {
            name: searchData.term
        }
    }).then(
        (resp) => {
            console.log('Search Data', resp);
            this.result.term = searchData.term;
            this.result.type = searchURI;
            deferred.resolve(resp.data[this._AppConstants[searchURI]['searchResp']]);

        },

        (err) => {
            console.log('Error performing search', err);
            deferred.reject(err.data);
        }
    );

    return deferred.promise;
}
4

1 回答 1

0

你正在使用

res as res.name for res in $ctrl.submit($viewValue)

后面的in应该是一个数组,或者是一个数组的承诺。

但事实并非如此。这是返回的内容$ctrl.submit()。而且这个方法不返回任何东西:

submit() {
    this.isSubmitting = true;

    // no return here

    this._SearchService.performSearch(this.search)
    .then(
        (resp) => {
            //e.g. [{id:1, name:'test'}]
            console.log('Search Result', resp);
            return resp;                                
        },

        (err) => {
            console.log('Error Search', err);
            this.reset(false);
            this.errors = err;
            return [];
        }
    );

    // no return here
}

唯一的 return 语句从传递给的函数返回,并在方法没有返回任何内容(即)之后异步then()执行。submit()undefined

因此,简而言之,您需要返回承诺:

return this._SearchService.performSearch(this.search) ...

请注意,如果您使用承诺链而不是解析/拒绝反模式,则您的服务方法可以减少和清洁:

performSearch(searchData) {
    console.log('Search Qry', searchData);

    if(!this.isValidSearch(searchData)) {
        return $q.reject({status:400,  error: 'Bad Request', message:'invalid data'});
    }

    let searchURI = (searchData.type === 'car' ? 'van' : 'issuer');
    return this._$http.get(`${this._AppConstants.api}/${this._AppConstants[searchURI]['search']}`,   { params: {name: searchData.term) } }).then(
        resp => {
            console.log('Search Data', resp);
            this.result.term = searchData.term;
            this.result.type = searchURI;
            return resp.data[this._AppConstants[searchURI]['searchResp']]);
        }).catch(resp => {
            console.log('Error Search', err);
            this.reset(false);
            this.errors = err;
            return $q.reject([]);
        });
}
于 2016-08-18T15:01:04.363 回答