使用bootstrap模式时,我无法使用角度js更改范围值。基本上我调用函数来打开我存储在controller.js文件中的模态,然后我试图修改范围值并在模态对话框中显示它们。
我将click事件添加到我的一个js组件中。
eventClick: function(event, element) {
var $scope = angular.element("#eRotaController").scope();
$scope.OpenModal("addEventModal", false, event);
}
部分html元素(模态对话框):
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{misc.modalTitles}}</h4>
</div>
最后是OpenModal函数和变量
// Variables
$scope.misc = {
modalTitle: "",
showRemoveEvent : false
};
$scope.OpenModal = function (modal, isNew, event)
{
if (isNew) {
$scope.misc.modalTitle = "Add new Event";
}
else{
$scope.misc.modalTitle = "Update event for: " + event.title;
$scope.misc.showRemoveEvent = true;
console.log($scope);
}
$("#" + modal).modal('show');
}
当我在OpenModal函数中更新modalTitle时,它在开启模态时没有反映任何变化。
我创建了一个更简单的例子来查明它是否存在模态问题并且结果不是。
我已添加的快速测试:
<button ng-click="OpenModal2()"></button>
{{misc.showRemoveEvent}}
$scope.OpenModal2 = function()
{
$scope.misc.modalTitle = "Update event for: " + event.title;
$scope.misc.showRemoveEvent = true;
}
工作正常。
所以当直接从html元素调用方法时它可以正常工作,但是当使用对控制器的引用从js调用时失败:
var $scope = angular.element("#eRotaController").scope();
$scope.OpenModal("addEventModal", false, event);
不起作用
答案 0 :(得分:1)
尝试做:
eventClick: function(event, element) {
var $scope = angular.element("#eRotaController").scope();
$scope.$apply(function (){
$scope.OpenModal("addEventModal", false, event);
})
},
如果eventClick是角度上下文之外的函数,则上述代码可能有效。
答案 1 :(得分:0)
尝试使用$uibModal
- $uibModal documation,它就像:
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.animationsEnabled = true;
//open modal
$scope.open = function (size) {
var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.toggleAnimation = function () {
$scope.animationsEnabled = !$scope.animationsEnabled;
};
});