【发布时间】:2014-07-28 19:21:10
【问题描述】:
我有一个自发停止工作的指令。出于某种原因,它永远不会被调用,并且控制台中不会打印出任何错误。这很奇怪,因为其他指令(似乎几乎相同)正在工作(请参阅帖子末尾的工作指令)。
这是指令:
angular.module('popup').directive('popup', ['Locator', 'PopupService', // This line of code is reached
function(Locator, PopupService) {
return {
restrict: 'A',
scope: {
"show": '=',
"anchor": '=',
'direction': '='
},
link: function($scope, element, attr) { // This never called
$scope.$watch('show', function(newValue, oldValue) {
if (newValue) { // This is never called
var pos = Locator.getCenterPosition($($scope.anchor));
PopupService.togglePopup($(element), {
x: pos.x,
y: pos.y,
origin: $scope.direction,
remove_callback: function() {
$scope.show = false;
console.log("SHOW: " + $scope.show);
}
});
} else {
autoHide();
}
}, true);
}
};
}
]);
这是包含指令的 Jade 代码(Jade 是一种 html 模板语言。):
block total-content
.div {{ edit }}
.main-body(ng-controller="editEditorController" ng-init="popups = {};format.colorMode='W'; draftID='#{draftID}'; draftEditorID='#{draftEditorID}'; draftOwnerID='#{draftOwnerID}' ")
div {{ commentEditor }}
ul#left-tool-list.side-tool-list.tool-list()
li#comments-tool-box
span.tool-box-title Comments
span.tool-box-control-area
#tool-box-controls
span#comment-button.tool-box-button(ng-click="newComment()") Add Comment
span#view-comments-button.tool-box-button(ng-init="popups.showCommentPopup = false" ng-click="popups.showCommentPopup = true; $event.stopPropogation();" stop-event='click') View Comments
div#comment-list-container(popup show="popups.showCommentPopup" anchor="'#view-comments-button'" direction="'top'") // The directive in question
comment-displayer#comment-list(edit="edit")
这是应用的声明:
var editEditorApp = angular.module('editEditorApp', ['general', 'API', 'popup']);
这是包含的顺序:
/* App */ script(src='/js/angular/editEditor/editEditorApp.js')
/* JQuery */ script(src='/js/pxem.JQuery.js')
/* Plain JS */ script(src='/styles/js/window-height.js')
/* Tinymce */ script(src='/js/ice_tinymce/tinymce/jscripts/tiny_mce/tiny_mce.js')
/* JQuery dep. */ script(src='/js/jquery.browser.min.js')
/* Angular Module - factory */ script(src='/js/angular/api/api.js')
/* Angular Module - directives */ script(src='/js/angular/directives/general.js')
/* Angular Module - popup (services) */ script(src='/js/angular/general/popupService.js')
/* Angular Module - popup (directive) */ script(src='/js/angular/directives/popup.js')
/* Angular Module */ script(src='/js/angular/filter/cut.js')
/* Angular Module - factory */ script(src='/js/angular/editEditor/commentLikeCreator.js')
/* Angular Module - factory */ script(src='/js/angular/editEditor/autoSave.js')
/* Angular Module - directives */ script(src='/js/angular/editEditor/commentBox.js')
/* Angular Module - directives */ script(src='/js/angular/editEditor/editor.js')
此指令有效,但我不知道为什么会这样:
editEditorApp.directive('commentBox',
function(PopupService) {
return {
restrict: 'E',
templateUrl: "/partials/edit-comment-box",
replace: true,
scope: {
"comment": '=',
"onDelete": '=',
"onHide": '=',
"location": '=',
"show": '='
},
link: function($scope, element, attr) {
console.log("LINK POPUP");
$scope.$watch('show', function(newValue, oldValue) {
console.log("NEW VALUE: " + newValue);
if (newValue) {
console.log("SHOW!");
$scope.popup = PopupService.popPopup($(element), {
x: location.x,
y: location.y,
origin: 'bottom',
hideOthers: true,
remove_callback: function() {
$scope.show = false;
console.log("SHOW: " + $scope.show);
}
});
} else {
if ($scope.popup) {
$scope.popup.removePopup();
}
}
});
},
controller: function($scope) {
console.log("CONTROLLER");
$scope.delete = function() {
$scope.popup.removePopup();
if ($scope.onDelete) {
$scope.onDelete();
}
};
$scope.hide = function() {
$scope.popup.removePopup();
if ($scope.onHide) {
$scope.onHide();
}
};
}
};
}
);
注意:这个问题之前在另一个问题下发布过,但我现在意识到不是指令的“监视”部分被破坏,而是指令从未被调用.我删除了上述问题并发布了这个问题。
【问题讨论】:
-
控制台是否报告任何内容,例如 Google Chrome 的开发工具 (F12)?
-
@stevuu 抱歉,我不清楚。当我说“没有错误”时,我的意思是控制台中没有报告错误。
-
最好看一下jade先编译好的html,看看有没有什么不同。您在此处显示的内容还不够清楚。特别是如果您确定它以前有效,那么一定是其他东西改变了它。我想你正在使用版本控制?如果是这样,只需恢复到有效的版本并从那里获取它。
-
你有没有试过改名字ie module('popup').directive('mypopup'
-
哪个文件声明了指令和服务共享的弹出模块?
angular.module('popup')将找到现有模块,但首先需要angular.module('popup', [])来声明它。如果以后使用该表单,它将覆盖原始模块而不会发出警告。
标签: javascript angularjs pug