使用controller as和vm在AngularJS中传递模态数据

时间:2016-08-02 22:15:07

标签: angularjs angular-ui-bootstrap

我有一对看起来像这样的控制器:

awk -F'&DEST='  '{
   printf("%s&DEST=", $1);
   xlen=index($2,"&");
   if ( xlen == 0) xlen=length($2)+1;
   for (i=0;i<xlen;i++) printf("%s", "X");
   endstr=substr($2,xlen);
   printf("%s\n", endstr);
   }' file

视图如下所示:

(function () {
    'use strict';

    angular
        .module('room')
        .controller('RoomGetCtrl', Room)
        .controller('TestCtrl', Test)

    Room.$inject = [...'$uibModal'...];
    Test.$inject = [...'$uibModalInstance'...];

    function Room(..., $scope, $uibModal) {
        var vm = this;

        ... 

        vm.open = function (size) {

            vm.modalInstance = $uibModal.open({
                templateUrl: 'modal.html',
                controller: 'TestCtrl as vm',
            });
        };
    }

    function Test(???){
        this.modalText = 'Modal Text'
        this.modalCancel = function() {
            ???.dismiss();
        }
    }
})();

上述工作,除了我无法弄清楚上面<script type="text/ng-template" id="modal.html"> <div class="modal-header"> <h3 class="modal-title">Modal window</h3> </div> <div class="modal-body"> <pre>{{ vm.modalText }}</pre> </div> <div class="modal-footer"> <button class="btn btn-default" ng-click="vm.modalCancel()">Cancel</button> </div> </script> <button type="button" class="btn btn-default" ng-click="vm.open()">Open me!</button> ... ???中的内容。我尝试过各种各样的东西,每当我点击取消按钮时,控制台都会按照“x.dismiss不是函数”的方式记录错误,其中“x”是我尝试过的任何东西。

任何帮助?

1 个答案:

答案 0 :(得分:0)

没关系。我立即自己解决了这个问题(我发誓,发布这个问题让我觉得更好)。无论如何,这里:

(function () {
    'use strict';

    angular
        .module('room')
        .controller('RoomGetCtrl', Room)
        .controller('TestCtrl', Test)

    Room.$inject = [...,'$uibModal'];
    Test.$inject = [$uibModalInstance];

    function Room(..., $uibModal) {
        /*jshint validthis: true */
        var vm = this;

        ...

        vm.open = function () {

            vm.modalInstance = $uibModal.open({
                templateUrl: 'modal.html',
                controller: 'TestCtrl as vm',
            });
        };
    }

    function Test($uibModalInstance){
        this.modalText = 'Modal Text'
        this.modalCancel = function() {
            $uibModalInstance.dismiss();
        }
    }
})();

真的很简单。 $uibModalInstance.dismiss();。它就在documentation

叹息