2

这是一个关于 Angular Materials、mdDialogs 和范围变量的问题:

  • 我使用 Stomp 订阅了特定主题。
  • Stomp 从服务器接收字符串,它们在范围变量中连接。
  • 用户单击按钮以显示 mdDialog。
  • mdDialog 应该在文本区域中显示传入的字符串更改。

但是……它不能正常工作。我必须关闭并重新打开对话框才能看到更改。我尝试在主视图(index.html)中添加 textarea 并且 textarea 工作正常。

当我们在 Angular Materials 的 mdDialog 中时,为什么它不改变 textarea?有什么办法解决吗?

这是您可以看到主视图(index.html)正确更新随机值的插件,但如果您打开对话框,该值将无法正确更新......

https://plnkr.co/edit/teC69Sg7UqNbouHxpT22

var angularInstance = angular.module('ExampleApp', ['ngMaterial', 'ngMessages']) ;

angularInstance.controller('ExampleCtrl', function ExampleCtrl($scope, $mdDialog, $mdMedia, $interval)
{
    $scope.randomString = "" ;

    $scope.initialization = function()
    {
      $interval($scope.addRandomChar, 1000) ; 
    }

    $scope.addRandomChar = function()
    {
      $scope.randomString = $scope.randomString + "a" ;
    }

  $scope.openMyDialog = function(ev)
    {
        var useFullScreen = ($mdMedia('sm') || $mdMedia('xs'))  && $scope.customFullscreen ;
        $mdDialog.show({
            controller: myDialogController,
            templateUrl: 'myDialog.tmpl.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose:true,
            fullscreen: useFullScreen,
            resolve: 
            {
                randomString: function ()
                {
                    return $scope.randomString ;
                }
            }
        }) ;
    }
});

function myDialogController($scope, $mdDialog, randomString) 
{
    $scope.randomString = randomString ;

    $scope.close = function ()
    {
        $mdDialog.cancel() ;
    };
}

非常感谢。

4

1 回答 1

2

Here you have a working plunker

Summarising, I've slightly changed the way you pass the params from the ExampleCtrl to myDialogController, using locals.

$mdDialog.show({
    controller: myDialogController,
    templateUrl: 'myDialog.tmpl.html',
    targetEvent: ev,
    locals: {parent: $scope},
    clickOutsideToClose:true,
    ...

Then, in your dialog controller, you have access all parent scope:

function myDialogController($scope, $mdDialog, parent) {
    $scope.parent = parent;

    ...
}

So finally, in the view you just need to bind parent.randomString to the textarea ng-model, and it will work as you expect:

<textarea ... ng-model="parent.randomString"/>

Cheers. Hope it helps.

于 2016-06-27T15:15:15.663 回答