我正在尝试将一个不断变化的作用域变量传递给模式窗口。模态打开后,我无法让变量更改。当前,当模态打开时,我有将数据传递给控制器:
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;
});
罗盘的Heading变量会不断更新,因此我试图获取罗盘的Heading变量以显示模态中的这些更改。
您可以使用具有您的变量以及其他变量的服务:
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
需要更改值,都可以使用normal:进行更改$scope.model.compassHeading = <some_value_here>;
。
同样,compassHeading
如果在控制器外部更改的值,则也将在控制器内部更改值,因为model
服务的对象是通过引用调用的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句