Angular controller scope not updating after jQuery ajax call

后端 未结 1 1418
南笙
南笙 2020-12-03 15:14

i have this code and i can\'t see where is the source of problem, i don\'t get any error in the chrome console my controller :

function notifController($sco         


        
相关标签:
1条回答
  • 2020-12-03 15:37

    Disregarding other architectural issues I pointed out in the comments, the real issue is that you're using jQuery's ajax instead of Angular's $http. When you don't do things like that through Angular, you're working outside of Angular's scope and it doesn't know about changes. While not ideal, you can use $scope.$apply to let angular know something was updated outside of its knowledge. That would look like this:

    $scope.$apply(function() {
      $scope.msgs = newMsgs;
    });
    

    That is telling Angular that you've modified something it needs to know about from a context that it doesn't know about (the jQuery ajax call in this case).

    There are some valid uses of $scope.$apply(), such as in event handlers, but most other times it is a sign of bad practices. You should definitely be using Angular's $http for ajax calls.

    0 讨论(0)
提交回复
热议问题