0
編輯:
請注意,這個問題是不重複在此question他們用$觀看控制器內,但我用它連接功能裏面。
我已經創建了一個包裝jstree jQuery插件指令,如下:
(function() {
'use strict';
angular.module('XYZ').directive('myTree', myTreeDirective);
myTreeDirective.$inject = ['$http', '$compile'];
var tree = {};
var config = {};
function myTreeDirective($http, $compile) {
function plugins(scope, element, attrs, config) {
//some code
return config;
}
function events(scope, element, attrs) {
//some code
return config;
}
function init(scope, element, attrs) {
plugins(scope, element, attrs, config);
tree = $(element).jstree(config);
events(scope, element, attrs);
}
/*
* Link function
*/
function linkFn(scope, element, attrs) {
$(function() {
config.core = {};
if (attrs.treeCore) {
config.core = $.extend(config.core, scope[attrs.treeCore]);
}
if (attrs.treeData === 'scope') {
scope.$watch('ngModel', function (n, o) {
if (n) {
config.core.data = scope.ngModel;
$(element).jstree('destroy');
init(scope, element, attrs, config);
}
}, true);
}
});
}
// expose directive
return {
restrict: 'E',
link: linkFn,
scope: {
ngModel: "=",
treeTypes: "=treeTypes"
}
};
}
})();
而且,由於我準備我的角2移民申請我正在尋找一個替代的$watch
功能:
scope.$watch('ngModel', function (n, o) {
if (n) {
config.core.data = scope.ngModel;
$(element).jstree('destroy');
init(scope, element, attrs, config);
}
}, true);
我想過使用Object.defineProperty
但是這是用來定義一個屬性,我在做什麼是當ngModel值改變爲執行代碼。
從谷歌搜索我發現我可以使用$onChanges
和$doCheck
生命週期鉤,但我不知道如何使用它們來替換上面的代碼。