我正在为多种目的构建一个 Angular 弹出系统。它的工作方式是我有一个指令,称为bitPopup
将三个变量传递给 (type
和action
) data
,如下所示:
索引.html
<bit-popup type="popup.type" action="popup.action" data="popup.data"></bit-popup>
popup.js
app.directive('bitPopup', function () {
return {
restrict: 'E',
template: html,
scope: {
type: '=',
action: '=',
data: '='
},
[***]
}
}
然后弹出控制器根据类型加载不同的指令:
popup.html (上面显示的 HTML 模板)
<div class="pop-up" ng-class="{visible: visible}" ng-switch="type">
<bit-false-positive-popup ng-switch-when="falsePositive" type="type" action="action" data="data"></bit-false-positive-popup>
</div>
false_positives.js (包含bitFalsePositivePopup
指令)
[...]
scope: {
type: '=',
action: '=',
data: '='
}
[...]
然后该bitFalsePositivePopup
指令的 html 模板显示来自data
.
现在我触发弹出窗口的方式如下:
- 从包含指令的指令内的模板中,我
bitPopup
将更改$scope.popup
'stype
和.action
data
- 我会去做的
$scope.$broadcast('showPopup');
- 该
bitPopup
指令将做出反应$scope.$on('showPopup', [...]});
,并使弹出窗口可见。
现在这个非常奇怪的事情发生在它第一次尝试的地方(弹出窗口打开并显示正确的data
信息),但在第一次尝试后它会显示data
上一次尝试的结果。
现在更奇怪的是,我尝试在第一次尝试时记录信息,而我发现:
$scope.popup
在index.html调用之前$scope.$broadcast('showPopup');
显示正确的信息。$scope.data
在bitPopup
指令显示null
$scope.data
atbitFalsePositivePopup
指令显示正确的信息。
第二次尝试:
$scope.popup
在index.html再次正确。$scope.data
atbitPopup
指令显示上一次尝试的信息bitFalsePositivePopup
指令也是如此。
另一个奇怪的事情是,当我使用$scope.$apply()
它时它确实工作正常,只有它显示$apply already in progress
错误。我知道我不应该$scope.$apply()
在这种情况下使用,因为它都是 Angular 事件。但是传递的范围怎么可能总是落后一步呢?
我一开始是不是做错了什么?
编辑:
由于amahfouz 的回答,我决定发布更多代码以进行澄清。我省略了一些不重要的细节,以便更清楚地阅读。
索引.html
<div class="falsePositives" ng-controller="falsePositives">
<i class="fa fa-minus color-red" ng-click="triggerPopup('falsePositive', 'delete', {detection: getDetection(row.detection, row.source), source: row.source, triggers: row.triggers, hash: row.hash, date: row.date})"></i>
<i class="fa fa-pencil" ng-click="triggerPopup('falsePositive', 'edit', {detection: getDetection(row.detection, row.source), source: row.source, triggers: row.triggers, hash: row.hash, date: row.date})"></i>
<bit-popup type="popup.type" action="popup.action" data="popup.data"></bit-popup>
</div>
index.js
var app = require('ui/modules').get('apps/falsePositives');
app.controller('falsePositives', function ($scope, $http, keyTools, bitbrainTools, stringTools) {
function init() {
$scope.getDetection = getDetection;
$scope.popup = {
type: null,
action: null,
data: null
};
}
function getDetection(hash, source) {
return {
'ids': 'BitSensor/HTTP/CSRF',
'name': 'CSRF Detection',
'description': 'Cross domain POST, usually CSRF attack',
'type': [
'csrf'
],
'severity': 1,
'certainty': 1,
'successful': false,
'input': ['s'],
'errors': []
};
}
$scope.triggerPopup = function (type, action, data) {
$scope.popup = {
type: angular.copy(type),
action: angular.copy(action),
data: angular.copy(data)
};
test();
$scope.$broadcast('showPopup');
};
function test() {
console.log('$scope.popup: ', $scope.popup);
}
}
popup.html
<div class="pop-up-back" ng-click="hidePopup()" ng-class="{visible: visible}"></div>
<div class="pop-up" ng-class="{visible: visible}" ng-switch="type">
<bit-false-positive-popup ng-switch-when="falsePositive" type="type" action="action" data="data"></bit-false-positive-popup>
</div>
popup.js
var app = require('ui/modules').get('apps/bitsensor/popup');
app.directive('bitPopup', function () {
return {
restrict: 'E',
template: html,
scope: {
type: '=',
action: '=',
data: '='
},
controller: function ($scope) {
$scope.visible = false;
$scope.$on('showPopup', function () {
console.log('$scope.data: ', $scope.data);
$scope.visible = true;
});
$scope.$on('hidePopup', function () {
hidePopup();
});
function hidePopup() {
$scope.visible = false;
}
$scope.hidePopup = hidePopup;
}
};
});
false_positives.js
var app = require('ui/modules').get('apps/bitsensor/falsePositives');
app.directive('bitFalsePositivePopup', function () {
return {
restrict: 'E',
template: html,
scope: {
type: '=',
action: '=',
data: '='
},
controller: function ($scope, objectTools, bitbrainTools, keyTools) {
function init() {
console.log('$scope.data @ fp: ', $scope.data);
}
function hidePopup() {
$scope.data = null;
$scope.$emit('hidePopup');
}
$scope.$on('showPopup', function () {
init();
});
init();
$scope.hidePopup = hidePopup;
}
}
}