Angularjs模态-如何在模态打开时动态设置模态内容
当模态打开/激活时,如何为模态内容设置新值 下面是示例代码Angularjs模态-如何在模态打开时动态设置模态内容,angularjs,dynamic,modal-dialog,Angularjs,Dynamic,Modal Dialog,当模态打开/激活时,如何为模态内容设置新值 下面是示例代码 angular.module('plunker', ['ui.bootstrap']); var ModalDemoCtrl = function ($scope, $modal, $log, $timeout) { $scope.test = "test variable" var modalInstance = $modal.open({ templateUrl: 'myModalContent.html
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log, $timeout) {
$scope.test = "test variable"
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
test: function () {
return $scope.test;
}
}
});
$timeout( function(){
$scope.test = "Hello World!";
}, 5000 );
};
var ModalInstanceCtrl = function ($scope, $modalInstance, test) {
$scope.test = test;
$scope.$watch('test',function (newValue, oldValue) {
$scope.test = test;
});
};
在父控制器中,我使用“test variable”初始化第一个模式,在超时(5秒)后,我希望模式变量在不关闭模式的情况下更改为“Hello World”
可以测试您可以直接将控制器作用域绑定到类似
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
scope: $scope
});
您可以直接将控制器作用域绑定到
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
scope: $scope
});
在ModaInstanceCtrl中使用$timeout函数,然后相应地更改变量的作用域值。以下是示例代码:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {
$scope.test = "test variable"
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
scope: $scope,
resolve: {
test: function () {
return $scope.test;
}
}
});
};
var ModalInstanceCtrl = function ($scope, $modalInstance, $timeout, test) {
$timeout(function(){
$scope.test = "variable"
}, 2000);
};
在ModaInstanceCtrl中使用$timeout函数,然后相应地更改变量的作用域值。以下是示例代码:
angular.module('plunker', ['ui.bootstrap']);
var ModalDemoCtrl = function ($scope, $modal, $log) {
$scope.test = "test variable"
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
scope: $scope,
resolve: {
test: function () {
return $scope.test;
}
}
});
};
var ModalInstanceCtrl = function ($scope, $modalInstance, $timeout, test) {
$timeout(function(){
$scope.test = "variable"
}, 2000);
};
我认为本例不需要
scope:$scope
,也不建议将其作为默认值,因此我建议将其从本例中删除或注释掉。我认为本例不需要scope:$scope
,也不建议将其作为默认值,因此,我建议将其从本例中删除或注释掉。