angularjs - controllerAs 在 ui-bootstrap 模式中没有绑定(bind)

标签 angularjs angular-ui-bootstrap bootstrap-modal

我正在将我的 Controller 重构为 controllerAs 作为我的模态。出于某种原因,我无法让模态以与 $scope

一起工作的方式与 controllerAs 一起工作

看起来就像设置一样简单 1.我在 Controller 中命名这个 2.在返回对象中调用controllerAs 3.绑定(bind)到 Controller :真 4. 将 ng-click 更改为 = ng-click="vm.ok()"

但是没有效果

这是带有 $scope 的原始文件。该指令工作得很好。

(function() {
    'use strict';

    angular
        .module('app.components.modal')
        .directive('myModal', myModal);



        function myModal() {
            return {
                restrict: 'E',
                scope: {},
                template: "<button class='btn btn-danger' ng-click='vm.open()'>Beer </button>",
                controller: ModalController,
                controllerAs: 'vm',
                bindToController: true
            }
        }

        function ModalController($modal, $log , $scope) {
            var vm = this;
            $scope.animationsEnabled = true;

            vm.open = open;

            function open() {
                var modalInstance = $modal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: 'app/components/modal/modal.html',
                    controller: ModalInstanceCtrl,
                    controllerAs: vm,
                    bindToController: true,
                    size: 'lg'
                    // resolve: {
                    //  title: function() {
                    //      return 'training Info';
                    //  }
                    // }            
                });
                modalInstance.result.then(function(selectedItem) {
                    console.log("Confirmed: "+ selectedItem);
                    $scope.selectedItem = selectedItem;
                }, function() {
                    $log.info('modal dismissed at: ' + new Date());
                });
            };
        }

        function ModalInstanceCtrl($scope, $modalInstance) {
            var vm = this;
            $scope.ok = function () {
                // console.log('beer', $scope.beer);
                // console.log('IBU',$scope.IBU);

                console.log('clicked');
               // $modalInstance.close($scope.selected.item);
               $modalInstance.close();
            };

            $scope.cancel = function () {
                console.log('clicked');
                $modalInstance.dismiss('cancel');
            };
        }



    })(); // end of iffe statement function

我的模式 html 文件代表 $scope。这个也可以

 <div class="modal-header" >
  <button type="button" class="close" ng-click="$hide()">&times;</button>
  <h4 class="modal-title" ></h4>
</div>
<div class="modal-body" >
  <form name="beerForm"> 
    <div class="form-group">
      <label> Beer Name</label>
      <input type="text" class="form-control" placeholder="beer" ng-model="beer.beerName">
    </div>
    <div class="form-group">
      <label> IBU</label>
      <input type="number" class="form-control" placeholder="IBU" ng-model="beer.IBU">
    </div>
  </form>
</div>
<div class="modal-footer">
  <button type="button" class="btn btn-default" ng-modal="beer.Name"ng-click="ok()">Cancel</button>
  <button type="button" class="btn btn-danger" ng-modal="beer.IBU" ng-click="cancel()">Confirm</button>
</div>

现在,我将 View 模型的 $scope 更改为 vm,并将 vm 添加到模态按钮,如下所示: vm.ok() vm.cancel() 然后按钮将不再起作用。

(function() {
    'use strict';

    angular
        .module('app.components.modal')
        .directive('myModal', myModal);



        function myModal() {
            return {
                restrict: 'E',
                scope: {},
                template: "<button class='btn btn-danger' ng-click='vm.open()'>Beer </button>",
                controller: ModalController,
                controllerAs: 'vm',
                bindToController: true
            }
        }

        function ModalController($modal, $log , $scope) {
            var vm = this;
            $scope.animationsEnabled = true;

            vm.open = open;

            function open() {
                var modalInstance = $modal.open({
                    animation: $scope.animationsEnabled,
                    templateUrl: 'app/components/modal/modal.html',
                    controller: ModalInstanceCtrl,
                    controllerAs: vm,
                    bindToController: true,
                    size: 'lg'
                    // resolve: {
                    //  title: function() {
                    //      return 'training Info';
                    //  }
                    // }            
                });
                modalInstance.result.then(function(selectedItem) {
                    console.log("Confirmed: "+ selectedItem);
                    $scope.selectedItem = selectedItem;
                }, function() {
                    $log.info('modal dismissed at: ' + new Date());
                });
            };
        }

        function ModalInstanceCtrl( $modalInstance) {
            var vm = this;
            vm.ok = function () {
                // console.log('beer', $scope.beer);
                // console.log('IBU',$scope.IBU);

                console.log('clicked');
               // $modalInstance.close($scope.selected.item);
               $modalInstance.close();
            };

            vm.cancel = function () {
                console.log('clicked');
                $modalInstance.dismiss('cancel');
            };
        }

    })(); // end of iffe statement function

似乎没有明确的理由表明 vm.ok() 不能在附加到模态的按钮上工作。我没有收到错误。

最佳答案

上面的代码几乎是正确的。我想使用 controllerAs。

函数 ModalController 有一个重大错误。我正在设置 var vm = this 。另外,我设置了 controllerAs: vm。我需要

************** 语录 *************

controllerAs: 'vm'   // this would have been the correct response.


function ModalController($modal, $log , $scope) {
        var vm = this;
        $scope.animationsEnabled = true;

        vm.open = open;

        function open() {
            var modalInstance = $modal.open({
                animation: $scope.animationsEnabled,
                templateUrl: 'app/components/modal/modal.html',
                controller: ModalInstanceCtrl,
                // here is where i should have controllerAs: 'vm'
                controllerAs: vm,
                bindToController: true,
                size: 'lg'
                // resolve: {
                //  title: function() {
                //      return 'training Info';
                //  }
                // }            
            });
            modalInstance.result.then(function(selectedItem) {
                console.log("Confirmed: "+ selectedItem);
                $scope.selectedItem = selectedItem;
            }, function() {
                $log.info('modal dismissed at: ' + new Date());
            });
        };
    }

关于angularjs - controllerAs 在 ui-bootstrap 模式中没有绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33061732/

相关文章:

angularjs - bootstrap-ui modal 不显示其背后的灰色背景,(打开时未添加modal-backdrop 类)

javascript - 在 html 表格单元格底部画一条线以反射(reflect)相对大小的策略

angularjs - Angular UI Bootstrap Tooltip 工具提示已打开奇怪的行为

javascript - 将 RAL 转换为 HEX 颜色代码

javascript - Angular JS - 提交到 $http 时的日期更改 - 时区问题

javascript - bootstrap-modal.js 在 Bootstrap 3 中损坏

javascript - 带 Canvas 的 Bootstrap 模态无法正确显示响应式设计

javascript - 在 Bootstrap 中放大图像 :- changes all images to enlarged image

javascript - AngularJS帮助: Connecting a Search Input into an REST Interface

javascript - 当您收到来自服务器的 blob 响应时,如何填充提示浏览器窗口?