0

我在我的 angularjs 中使用 jquery fancytree 插件,我正在尝试使用“激活”事件来调用 $scope 内的函数。如果我断点代码 - 它看起来不错,但它没有显示在 html 中。

这是我的代码:

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}}

任何想法为什么?

4

1 回答 1

2

由于您的事件是在角度“外部”触发的,因此您需要手动刷新范围:

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

有关更多信息,请参见此处:https ://docs.angularjs.org/guide/scope

于 2014-09-15T10:00:23.087 回答