angularjs binding in scope not in ui

扶醉桌前 提交于 2019-12-13 05:03:35

问题


i am using jquery fancytree plug in in my angularjs and i am trying to use the 'activate' event to call a function inside the $scope. if i breakpoint the code - it looks good , but its not showing in the html.

here is my code:

app.controller('ctrl', ['$scope', 'treeSvc', function ($scope, treeSvc) {

  $scope.selectedNode = null;

  $scope.SetSelectedNode = function(newNode){
    $scope.selectedNode = newNode; 
    //a break point here shows that  $scope.selectedNode is really changing but its not showing in the html.  calling this function outside the fancy tree - works.
  }

  treeSvc.get()
    .then(function (response) {
      $("#tree").fancytree({
        source: response.data,
        activate: function(event, data) {
          $scope.SetSelectedNode(data.node);
        }
      });
    },
    function () {
      alert('error getting tree data');
    });

}]);

Html

{{selectedNode | json}}

any ideas why?


回答1:


As your event is fired "outside" of angular, you need to refresh the scope manually :

$("#tree").fancytree({
  source: response.data,
  activate: function(event, data) {
    $scope.SetSelectedNode(data.node);
    $scope.$apply();
  }
});

See here for more infos : https://docs.angularjs.org/guide/scope



来源:https://stackoverflow.com/questions/25845362/angularjs-binding-in-scope-not-in-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!