简体   繁体   English

AngularJS双向数据绑定

[英]AngularJS Two way data binding

In my controller I have the following data: 在我的控制器中,我有以下数据:

(function() {
'use strict';

angular
    .module('myappl.mymodule')
    .controller('MyController', MyController);

MyController.$inject = ['$scope', 'myService'];

function MyController($scope, 'myService') {
    $scope.vm = this; 
    var vm = this;

    vm.myService = myService;

    vm.userManagement = userManagement.data;
    vm.userManagementSomeDataObjects = vm.userManagement.someDataObjects;

Somewhere in this controller I have a function which first gets data from backend and than invoke showModal : 在此控制器的某个地方,我有一个函数,该函数首先从后端获取数据,然后调用showModal

function modalForUserInteraction() {
        vm.myService.getData(parameters).success(function(data) {
            vm.modalService.showModal(data, vm.userManagement, vm.userManagementSomeDataObjects);
        }).error(function(data) {
            console.log('error');
        });
    }

The modal- controller looks like this: 模态控制器如下所示:

...
function showModalService($modal, $stateParams, otherService) {
    var service = {
        showModal: showModal
    };      
    return service;

    ////////////

    function showModal(data, userManagement, userManagementSomeDataObjects) {
         var myModal = $modal.open({
            controller: ModalController,
            controllerAs: 'vm',
            windowClass: "modal fade in",
            resolve: {  
                userManagement: function() {
                return userManagement;
            },
            userManagementSomeDataObjects: function() {
                return userManagementSomeDataObjects;
            }
            },  
            templateUrl: 'url/to.html'
        });
        return myModal;

and in the modal controller there is a method like this one: 在模态控制器中有一种类似这样的方法:

function ModalController(userManagement, userManagementSomeDataObjects) {
var vm = this;

...

function doSomeActionAfterButtonClickAtModal() {
    otherService.getDataFromBackend(params).success(function(data) {

        userManagement = data;
            userManagementSomeDataObjects = data.someDataObjects;

})error(function(data) {
    console.log('error');
});
}

If I do it like this: 如果我这样做:

userManagement = data; userManagement =数据; and userManagementSomeDataObjects = data.someDataObjects; userManagementSomeDataObjects = data.someDataObjects; than the new data is not set. 比未设置新数据。

If I set each property separately of the objects than it works more often than not but somethimes it does not. 如果我将每个属性分别与对象分开设置,那么它经常会比某些事物更有效,但某些事物则不会。

My question now would be what I can do in order to get it work. 现在我的问题是,我应该怎么做才能使其正常工作。 Currently I do not have a $scope- variable in my modal and actually I don't know if $scopeOfModal.$apply() would help and I also don't know how to get access from modal to MyController - $scope . 目前,我的模态中没有$ scope-变量,实际上我不知道$ scopeOfModal。$ apply()是否会有所帮助,我也不知道如何从模态访问MyController-$ scope

I would be glad for any hint in this direction. 我很高兴向这个方向提供任何提示。

Thanks a lot! 非常感谢!

[EDIT] Here is an image of my currently viewed (right) an on the left side the object, which should be shown after setting in modal- function. [编辑]这是当前查看的图像(右侧)和对象的左侧,应在模态功能中设置后显示。

在此处输入图片说明

[EDIT] [编辑]

is there any posibility to pass parameters to this function in the modal controller: 是否可以在模态控制器中将参数传递给此函数:

this.previewArchivedSchedule = function(hereINeedParamerts) {
             alert('archivedScheduleIntervalContainerId: ' + hereINeedParamerts); 
          };

This looks to me just like it may have nothing to do with angular, just some confusion with javascript variable references. 在我看来,这似乎与angular没有任何关系,只是与javascript变量引用有些混淆。

First you pass userManagement from showModalService.showModal to ModalController via resolve . 首先你通过userManagementshowModalService.showModalModalController通过resolve

So now ModalController has a reference to the same object as showModalService . 因此,现在ModalController可以引用与showModalService相同的对象。

However, in ModalController , you reassign the userManagement variable to point to data instead. 但是,在ModalController ,您可以重新分配userManagement变量以指向data So now the userManagement variable inside ModalController isn't pointing at the injected object anymore, because you've reassigned it. 所以,现在的userManagement内部变量ModalController不是在被注入的对象指向了,因为你已经重新分配它。 This has nothing to do with angular two-way data binding, it's just javascript variable assignment. 这与角度双向数据绑定无关,只是javascript变量分配。 You've lost your reference to the original object. 您已失去对原始对象的引用。

showModalService still has a reference to the instance that it sent in via resolve, it has no idea that you swapped the reference out in the ModalController . showModalService仍然对通过resolve发送的实例具有引用,它不知道您已在ModalController换出了该ModalController

I'd try sending over an object encapsulating the data you want to share to fix this problem. 我会尝试发送一个封装了您要共享的数据的对象,以解决此问题。

function showModal(data, userManagement, userManagementSomeDataObjects) {
     var myModal = $modal.open({
        controller: ModalController,
        controllerAs: 'vm',
        windowClass: "modal fade in",
        resolve: {  
            sharedData: function() {
                return {
                    userManagement: userManagement,
                    userManagementSomeDataObjects: userManagementSomeDataObjects
                }
        }, 
        templateUrl: 'url/to.html'
    });
    return myModal;

Then manipulate the properties on the shared object instead of overwriting references. 然后操作共享对象上的属性,而不是覆盖引用。

function ModalController(sharedData) {
var vm = this;

...

function doSomeActionAfterButtonClickAtModal() {
    otherService.getDataFromBackend(params).success(function(data) {
        sharedData.userManagement = data;
        sharedData.userManagementSomeDataObjects = data.someDataObjects;
    })error(function(data) {
        console.log('error');
    });
}

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

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