简体   繁体   English

Angular $ scope.variable undefined

[英]Angular $scope.variable undefined

I am having a problem that my $scope.todo list always returns a undefined when using the angular function ng-repeat. 我遇到的问题是,当使用角度函数ng-repeat时,我的$ scope.todo列表总是返回undefined。 If I define the $scope.todo it works perfectly but when I use the solution below to fetch results and add it to the variable I get an undefined before it seems to have a chance to go and retrieve the correct values 如果我定义$ scope.todo它可以很好地工作,但是当我使用下面的解决方案获取结果并将其添加到变量时,我得到一个未定义的,它似乎有机会去检索正确的值

I have now added a bit better code to explain my problem. 我现在添加了一些更好的代码来解释我的问题。 After looking at some of the jsfiddles below and trying those solutions I'm starting to think its something to do with my callbacks. 在查看下面的一些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);
    });
}

Shouldn't this: 不应该这样:

$scope.testList = buildInitialList(function (result){
     return result;
   }

Be like this: 是这样的:

$scope.testList = buildInitialList( function (result){return result;} );

And buildInitialList function is not returning any value. 而且buildInitialList函数没有返回任何值。 Based on your sample code it could something like this: 根据您的示例代码,它可能是这样的:

function buildInitialList(callback){
   var result = doWorkAndGetResult();
   callback(result);
  //missing return value...
   return result; /*maybe?*/
}

Here is a fully working jsfiddle demo: 这是一个完全有效的jsfiddle演示:

http://jsfiddle.net/f9ee4/1/ http://jsfiddle.net/f9ee4/1/

You are never actually assigning your result to the scope variable. 您实际上从未将结果分配给范围变量。 Your callback is being called, but the return value of your callback is NOT what gets assigned to your scope property. 正在调用您的回调函数,但回调的返回值不是分配给您的scope属性的值。

By using a callback, I'm assuming you have some sort of async call in that global function. 通过使用回调,我假设你在该全局函数中有某种异步调用。 If you don't have an async call in there, then you should just return the value from buildInitialList, without using a callback. 如果你没有异步调用,那么你应该只返回buildInitialList中的值,而不使用回调。

Here's a fiddle that works: http://jsfiddle.net/973nW/ 这是一个有效的小提琴: http//jsfiddle.net/973nW/

function MyCtrl($scope) {
    buildInitialList(function (result){
        $scope.name = result;
    });  
}

Side note: using a global function isn't a good idea, you may want to consider putting your function into a service. 附注:使用全局函数不是一个好主意,您可能需要考虑将您的函数放入服务中。

After pulling my hair out and thinking something was wrong with my javascript it turns out the problem was actually within the ng-repeat I was using in the HTML 把我的头发拉出来并且认为我的javascript出了问题之后,事实证明问题实际上是在我在HTML中使用的ng-repeat中

This is what my html looked like 这就是我的html看起来像

<ul class="unstyled">
        <li ng-repeat="todo in todos">
            <input type="checkbox" ng-model="todo.done">
            <span class="done-{{todo.done}}">{{todo.text}}</span>
        </li>
</ul>

This worked fine when I had defined a list of ToDo's right from the start however when I had to go to the DB & retrieve that list the results never seemed to be applied to the original $scope.todos 当我从一开始就定义了ToDo的列表时,这很好用,但是当我不得不去DB并检索该列表时,结果似乎从未应用到原始$ scope.todos

I had to use the $scope.$apply function to make sure when my values were retrieved from the DB they were actually applied to the $scope afterwards 我必须使用$ scope。$ apply函数来确保从数据库中检索到我的值后,它们实际应用于$ scope

buildInitialList(function (result){
    $scope.$apply(function (){
        console.log(result);
        $scope.todos = result;
    });
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM