【发布时间】:2016-04-04 19:13:48
【问题描述】:
我正在为多种目的构建一个 Angular 弹出系统。它的工作方式是我有一个名为bitPopup 的指令,其中三个变量被传递给(type、action 和data),如下所示:
index.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的type、action和data。 - 我会做
$scope.$broadcast('showPopup'); -
bitPopup指令会因为$scope.$on('showPopup', [...]});而做出反应,并使弹出窗口可见。
现在这个非常奇怪的事情发生在第一次尝试时(弹出窗口以正确的data 信息打开),但在第一次尝试后它将显示上一次尝试的data。
现在更奇怪的是,我尝试在第一次尝试时记录信息,结果发现:
-
index.html 上的
$scope.popup就在调用$scope.$broadcast('showPopup');之前会显示正确的信息。 -
$scope.data在bitPopup指令处显示null -
bitFalsePositivePopup指令中的$scope.data显示正确的信息。
第二次尝试:
-
index.html 处的
$scope.popup再次正确。 -
bitPopup指令中的$scope.data显示上一次尝试的信息 -
bitFalsePositivePopup指令也是如此。
另一个奇怪的事情是,当我使用$scope.$apply() 时,它确实可以正常工作,只是它显示$apply already in progress 错误。我知道在这种情况下我不应该使用 $scope.$apply(),因为这都是 Angular 事件。但是传递的范围怎么可能总是落后一步呢?
我一开始是不是做错了什么?
编辑:
由于amahfouz's answer,我决定发布更多代码以进行澄清。为了更清晰的阅读,我省略了一些不重要的细节。
index.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;
}
}
}
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-scope angular-broadcast