当我更改此“jstree”指令代码中的范围值时(指令链接函数 - 添加一个插件来重绘jstree节点,该节点向树中的每个项添加按钮,并且onclick用于模式更改):
link: function(scope, element, attrs) {
scope.selectedNode = scope.selectedNode || {};
scope.treeUrl = scope.treeUrl || 'treeUrl';
var treeElement = $(element);
var link = document.createElement('i');
//jstree plugin to rewrite node
$.jstree.plugins.noderewrite = function (options, parent) {
this.redraw_node = function (obj, deep, callback) {
obj = parent.redraw_node.call(this, obj, deep, callback);
if (obj) { //obj is listitem of the tree
var tmp = link.cloneNode(true);
tmp.href = "javascript:void(0)";
tmp.className = "glyphicon-pencil";
tmp.style = "cursor: pointer;";
tmp.onclick = function () {
scope.mode = "edit";
}
obj.insertBefore(tmp, obj.childNodes[2]);
var tmp2 = link.cloneNode(true);
tmp2.href = "javascript:void(0)";
tmp2.className = "glyphicon-plus";
tmp2.style = "cursor: pointer;";
tmp2.onclick = function () {
scope.mode = "add";
}
obj.insertBefore(tmp2, obj.childNodes[2]);
}
未调用控制器中的监视代码:
var TreeController = [
'$scope', 'stateService', '$timeout',
function ($scope, stateService, $timeout) {
$scope.treeUrl = "...";
$scope.selectedTreeNode = {};
$scope.mode = 'view';
var previousSelection;
$scope.watch('mode', function (name, oldVal, newVal) {
stateService.go(newVal);
});
浏览器之间也存在一种奇怪的行为。在Firefox 36.0.1(Windows)一切正常。在firefox中可以使用$ scope.watch而不用$,但在chrome中我得到错误。手表中的代码不会在chrome中调用。 我试过深刻的观察,例如。看第三个参数是真的,没有结果。 可能使用手表不是一个好主意,而是我应该将我的状态服务注入指令并直接使用它而不是手表。也许我只是没有采用Angular的方式。