简体   繁体   English

如何将变化的数据传递给模态?

[英]How do i pass changing data into a modal?

I'm trying to pass a changing scope variable into a modal window. 我正在尝试将变化的作用域变量传递给模式窗口。 I'm having trouble allowing the variable to change once the modal is open. 模态打开后,我无法让变量更改。 Currently I have the data being passed to a controller when the modal is opened: 当前,当模态打开时,我有将数据传递给控制器​​:

    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;
                }
            }
        });
    };

and my controller: 和我的控制器:

angular.module('TestApp').controller('ModalInstanceCtrl',function ($scope, $modalInstance, items, compassHeading) {

    'use strict';

    $scope.items = items;
    $scope.selected = {
        item: $scope.items
    };
    $scope.compassHeading = compassHeading;

});

The compass Heading variable is constantly being updated, so I am trying to get the compassHeading variable to show these changes in the modal. 罗盘的Heading变量会不断更新,因此我试图获取罗盘的Heading变量以显示模态中的这些更改。

You could use a service which has your variable along with other variables: 您可以使用包含变量和其他变量的服务:

angular.module('TestApp')
    .service('TestService', [function () {
        return {
            model: {
                'compassHeading': null
            }
        };
    }]);

And in your main controller, you could use it like: 在主控制器中,您可以像这样使用它:

angular.module('TestApp')
    .controller('MainController', ['$scope', 'TestService', function ($scope, testService) {
        $scope.model = testService.model;
        ...
    }]);

And in your $modal controller, you could do the same: $modal控制器中,您可以执行以下操作:

angular.module('TestApp')
    .controller('ModalInstanceCtrl', ['$scope', 'TestService', function ($scope, testService) {
        $scope.model = testService.model;
    }]);

Then, anytime the value of compassHeading needs to be changed, you could just change it using the normal: $scope.model.compassHeading = <some_value_here>; 然后,每当需要更改compassHeading的值时,都可以使用常规方法更改它: $scope.model.compassHeading = <some_value_here>; .

Also, the value of compassHeading , if changed outside the controllers, will also be changed inside the controllers, since the model object of the service is called by reference. 同样,如果在控制器外部更改了compassHeading的值,那么在控制器内部也会更改compassHeading的值,因为该服务的model对象是通过引用调用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM