简体   繁体   English

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

[英]Passing modal data in AngularJS using controller as and vm

I have a pair of controllers that looks like this: 我有一对看起来像这样的控制器:

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

The view looks like this: 该视图如下所示:

<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>
...

The above works, except that I'm unable to figure out what goes in the ??? 上面的作品,除了我无法弄清楚??? in Test() above. 在上面的Test() I've tried a variety of things, whenever I click the cancel button, the console logs an error along the lines of "x.dismiss is not a function," where "x" is whatever I've tried use. 我已经尝试了多种方法,每当我单击“取消”按钮时,控制台都会在“ x.dismiss不是一个函数”的行中记录错误,其中“ x”是我尝试使用的内容。

Any help? 有什么帮助吗?

Nevermind. 没关系。 I instantaneously worked it out myself (I swear, posting the question makes me think better). 我立刻自己解决了(我发誓,发布问题使我的想法更好)。 Anyway, here: 无论如何,在这里:

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

Really simple. 真的很简单。 $uibModalInstance.dismiss(); . It was right there in the documentation 就在文档中

sigh

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

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