我试图将更改范围变量传递到模态窗口。一旦模态打开,我就无法更改变量。目前,我在打开模态时将数据传递给控制器:
scope.open = function (roomname, image) {
console.log("clicked modal");
console.log("roomName: " + roomname);
console.log("image: " + image);
scope.imageContents = image;
console.log("scope.imageContents: " + scope.imageContents);
scope.modalInstance = $modal.open({
animation: scope.animationsEnabled,
templateUrl: 'tpl/modal-template.tpl.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function () {
console.log("scope.imageContents in resolve: " + scope.imageContents);
return scope.imageContents;
},
compassHeading: function () {
console.log("scope.imageContents in resolve: " + scope.compassHeading);
return scope.compassHeading;
}
}
});
};
和我的控制员:
angular.module('TestApp').controller('ModalInstanceCtrl',function ($scope, $modalInstance, items, compassHeading) {
'use strict';
$scope.items = items;
$scope.selected = {
item: $scope.items
};
$scope.compassHeading = compassHeading;
});
指南针标题变量不断更新,所以我试图获取compassHeading变量来显示模态中的这些变化。
答案 0 :(得分:0)
您可以使用包含变量和其他变量的服务:
angular.module('TestApp')
.service('TestService', [function () {
return {
model: {
'compassHeading': null
}
};
}]);
在您的主控制器中,您可以像以下一样使用它:
angular.module('TestApp')
.controller('MainController', ['$scope', 'TestService', function ($scope, testService) {
$scope.model = testService.model;
...
}]);
在你的$modal
控制器中,你也可以这样做:
angular.module('TestApp')
.controller('ModalInstanceCtrl', ['$scope', 'TestService', function ($scope, testService) {
$scope.model = testService.model;
}]);
然后,只要需要更改compassHeading
的值,您就可以使用法线更改它:$scope.model.compassHeading = <some_value_here>;
。
此外,compassHeading
的值(如果在控制器外部更改)也将在控制器内部更改,因为服务的model
对象是通过引用调用的。