我有一個問題,即當使用角函數ng-repeat時,我的$ scope.todo列表總是返回一個未定義的值。如果我定義$ scope.todo,它可以很好地工作,但是當我使用下面的解決方案獲取結果並將其添加到變量中時,我會得到一個未定義的數據,它似乎有機會去檢索正確的值
I現在增加了一些更好的代碼來解釋我的問題。在看了下面的一些jsfiddles並嘗試這些解決方案之後,我開始認爲它與我的回調有關。
function TodoCtrl($scope) {
$scope.todos = [];
buildInitialList(function (result){
console.log(result); //Logs defined after the undefined below
$scope.todos = result;
});
console.log($scope.todos); //Logs undefined before the defined log above
}
function buildInitialList(callback){
//Simulates call to db
setTimeout(function(){
callback([{text: 'item 1', done: false},
{text: 'item 2', done: false}]);
},2000);
}
function fetch(url, callback) {
$.getJSON(url, function (data, status) {
callback(data);
});
}
數據庫中檢索到的我的價值觀有幾個語法錯誤是他們拼寫錯誤或實際代碼? –
對不起,只是錯別字:( –
你實際上從來沒有真正將結果賦值給你的範圍變量http://jsfiddle.net/973nW/ –