[英]Uncaught TypeError: Cannot read property 'sheet' of null
我正在學習AngularJS,並在更改此函數后遇到問題:
gList : function (){
return List
},
有了這個:
gList : function() {
$http({method:'GET',url:'/sessions'}).
success(function(data){
return data;
}).
error(function(data){
$log.info(data);
})},
現在,我收到此錯誤:
Uncaught TypeError: Cannot read property 'sheet' of null
(anonymous function) modernizr-2.6.2.min.js:4
y modernizr-2.6.2.min.js:4
s.fontface modernizr-2.6.2.min.js:4
(anonymous function) modernizr-2.6.2.min.js:4
(anonymous function) modernizr-2.6.2.min.js:4
該函數在這里調用:
$scope.sessions = Sessions.gList();
然后
ng-repeat="session in sessions"
雖然我什至不在代碼中使用工作sheet
。 救命?
這里的問題是在您的http請求得到響應的時間內,腳本中的ng-repeat會話已被調用。 這意味着session的值為null,我認為您嘗試在未定義的HTML中訪問{{session.sheet}},因此瀏覽器會告訴您。 解決此問題的方法是將$ q.defer.resolve()和$ q.defer.reject()用於http響應和錯誤。 而不是通過$ q.promise。 下面是一個示例:
.service('httpService' , ['$http','$q', function($http, $q) ]{
$http(sendConfig).then(function(response) {
deferred.resolve(response);
}, function(response) {
deferred.error(response);
}, function(message) {
deferred.notify(message);
});
return deferred.promise;
});
並在控制器中嘗試訪問此
httpService().then(function(res){
$scope.sessions = res;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.