我在我的angularjs中使用jquery fancytree插件 我正在尝试使用'激活' event调用$ 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}}
任何想法为什么?
答案 0 :(得分:2)
当您的事件在角度“外部”触发时,您需要手动刷新范围:
$("#tree").fancytree({
source: response.data,
activate: function(event, data) {
$scope.SetSelectedNode(data.node);
$scope.$apply();
}
});
有关更多信息,请参阅此处:https://docs.angularjs.org/guide/scope