在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/人工智能  HTML/ angularjs 控制器中用Promise初始化數(shù)據(jù)時(shí),model綁定不成功。

angularjs 控制器中用Promise初始化數(shù)據(jù)時(shí),model綁定不成功。

在controller的初始化數(shù)據(jù)方法里用Promise綁定數(shù)據(jù)

controller init方法中,用storage返回的Promise初始化數(shù)據(jù):

        storage.getCategoryList().then(function (res) {
            $scope.categoryList = res;
        });

storage組件中,先從sessionStorage中取數(shù)據(jù),沒取到再用APIService調(diào)接口從后臺(tái)取數(shù)據(jù),再將結(jié)果返回:

storage.getCategoryList = function () {
        return new Promise(function (resolve, reject) {
            let categoryList = sessionStorage.getItem('categoryList');
            if(categoryList == null || categoryList === undefined){
                APIService.getCategoryList().then(function (res) {
                    if(res.data.httpStatus === 200){
                        categoryList = res.data.categoryList;
                        sessionStorage.categorylist = categoryList;
                        resolve(categoryList);
                    }else{
                        reject();
                    }
                });
            }else{
                resolve(categoryList)
            }
        })
    }

前端html:

<div class="form_items_content_input form_items_content input-group">
                <input type="text" class="form-control" name="category" ng-model="category">
                <div class="input-group-btn">
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                        選擇分類
                        <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                        <li ng-repeat="a in categoryList" ng-click="bindingCategory(a)"><a>{{a}}</a></li>
                    </ul>
                </div><!-- /btn-group -->
            </div>

這樣子取數(shù)據(jù)后,categroyList綁定失?。?/p>

clipboard.png

將controller init方法改為直接從后臺(tái)取數(shù)據(jù)卻能成功:

APIService.getCategoryList().then(function (res) {
            if(res.data.httpStatus === 200){
                $scope.categoryList  = res.data.categoryList;
            }
        })

clipboard.png

在前面的方法中調(diào)用一次$apply更新數(shù)據(jù)也能成功:

storage.getCategoryList().then(function (res) {
            //此處直接賦值,數(shù)據(jù)無法綁定到view,需用$apply
            $scope.$apply(function () {
                $scope.categoryList = res;
            })
        });

于是我就想到可能是因?yàn)閍ngularjs數(shù)據(jù)綁定已經(jīng)完成了,第一種方法比較慢,還沒將值賦給$scope.categoryList,才導(dǎo)致綁定失敗,就將兩種方法的執(zhí)行時(shí)間比較了一下:

let start = new Date()
        storage.getCategoryList().then(function (res) {
            //此處直接賦值,數(shù)據(jù)無法綁定到view,需用$apply
            $scope.$apply(function () {
                $scope.categoryList = res;
                console.log(new Date().getTime() - start.getTime())
            })
        });
let start = new Date()
        APIService.getCategoryList().then(function (res) {
            if(res.data.httpStatus === 200){
                $scope.categoryList  = res.data.categoryList;
                console.log(new Date().getTime() - start.getTime())
            }
        })

結(jié)果發(fā)現(xiàn)前面的方法執(zhí)行時(shí)間大概率優(yōu)于后者,所以感到十分好奇,為什么前面的方法沒法綁定數(shù)據(jù),必須得用$apply才行??

有沒有大神能幫忙解答一下這問題,感激不盡!

回答
編輯回答
安淺陌
2018年4月14日 12:50
編輯回答
久愛她

脫離angular上下文,需要使用$apply強(qiáng)制數(shù)據(jù)同步??
在angular中調(diào)用非scope下的函數(shù),如window下的方法,也是需要$apply才能夠強(qiáng)制同步

2018年9月1日 04:06