【发布时间】:2017-12-18 22:14:08
【问题描述】:
我正在开发一个 AngularJS 应用程序,我的任务类似于:
1) 点击一个按钮。
2) 如果variableA === true,则输出一个模态。
3) 如果 variableA === false,请转到另一个链接而不弹出模式。
现在,我正在尝试使用以下代码来实现它
HTML:
<span id = "id1" ng-click="getClickType($event,row)" href=""> Text </span>
JS:
$scope.getClickType = function(event,row){
$scope.activeElement = event.target;
if(row.type == "A"){
$scope.activeElement.dataset.target = "#ModalA";
$scope.activeElement.dataset.toggle = "modal";
/* Here I don't have to use the hack and the data target changes automatically and the modal pops up*/
}
else{
someFunction().then(someOtherFunction);
}
}
someOtherFunction(msg){
if (msg.varA == true){
var f1 = $scope.activeElement.dataset.target; //hack
$scope.activeElement.dataset.target = "#ModalB";
$scope.activeElement.dataset.toggle = "modal";
if( f1 != $scope.activeElement.dataset.target){ //hack If statement
$timeout(function () {
$scope.activeElement.click();
});
/* Here i have use a hack and click on the element again to show the modal*/
}
}
else{
$scope.activeElement.dataset.target = "";
$scope.activeElement.dataset.toggle = "";
window.open(someURL);
/* works fine*/
}
}
所以你可以看到,我正在使用 ModalB 的 hack。更改 someOtherFucntion() 中的 dataset.target 不会弹出模式。我必须再次单击活动元素才能显示。
我的问题是:
为什么我需要再次点击activeElement才能更改数据目标。
为什么只需要在第二个函数中。
任何帮助将不胜感激。
提前致谢。
【问题讨论】:
标签: javascript angularjs twitter-bootstrap