【问题标题】:AngularJS: "TypeError: Cannot call method '$broadcast' of null"AngularJS:“TypeError:无法调用 null 的方法‘$broadcast’”
【发布时间】:2014-02-25 17:12:15
【问题描述】:

我是 AngularJS 的新手,正在开发我的第一个移动应用程序,从 Ionic 框架开始。我有一个部分让用户点击“写评论”按钮。这反过来会打开一个 Ionic 模式,该模式附加了一个表单。当用户填写表格并点击帖子时,我将使用该数据做一些事情,然后它调用的函数会破坏模式窗口。提交表单后抛出错误,每次打开写评论按钮时我都有数据。

打开模式的代码:

  <a ng-controller="openReviewCtrl" ng-click="openModal()" class="item item-icon-left">
    <i class="icon ion-compose"></i>
    Write a Review (10 POINTS)
  </a>

审核表单模板:

<div class="modal" >
  <header class="bar bar-header bar-positive">
    <h1 class="title">Write Review</h1>
    <button class="button button-clear button-positive" ng-click="closeModal()">Cancel</button>
  </header>
  <ion-content has-header="true" padding="true">
    <form class="list">
      <label class="item item-input">
        <input type="text" placeholder="Full Name" ng-model="review.author"  name="fullName">
      </label>
      <label class="item item-input">
        <textarea placeholder="Review" cols="50" ng-model="review.text"  name="review"></textarea>
      </label>
      <button class="button button-full button-positive"  ng-click="writeReview(review)" />Post</button>
    </form>
  </ion-content>
</div>

最后是我的控制器代码(目前只是记录数据):

.controller('openReviewCtrl', function($scope, $ionicModal) {
    $scope.master = {};
  $ionicModal.fromTemplateUrl('templates/review.html', function(modal) {
    $scope.modal = modal;
  }, {
    scope: $scope,
    animation: 'slide-in-up'
  });

  $scope.openModal = function() {
    $scope.modal.show();
  };
  $scope.closeModal = function() {
    $scope.modal.hide();
  };
  //Be sure to cleanup the modal
  $scope.$on('$destroy', function() {
    $scope.modal.remove();
  });
     $scope.writeReview = function(review) {
        console.log(review);
        $scope.modal.remove();
  };
})

点击“写评论”模式打开。
数据被写入并提交。
记录写得很好并关闭 Modal。
再次单击“撰写评论”时会引发错误
发布按钮重置页面和取消不起作用

TypeError: Cannot call method '$broadcast' of null
    at ionic.views.Modal.inherit.show (http://angular.dev:8888/www/lib/js/ionic.bundle.js:31554:26)
    at Scope.$scope.openModal (http://angular.dev:8888/www/js/controllers.js:49:18)
    at http://angular.dev:8888/www/lib/js/ionic.bundle.js:17182:21
    at http://angular.dev:8888/www/lib/js/ionic.bundle.js:33774:11
    at Scope.$eval (http://angular.dev:8888/www/lib/js/ionic.bundle.js:18939:28)
    at Scope.$apply (http://angular.dev:8888/www/lib/js/ionic.bundle.js:19039:23)
    at HTMLAnchorElement.<anonymous> (http://angular.dev:8888/www/lib/js/ionic.bundle.js:33773:15)
    at http://angular.dev:8888/www/lib/js/ionic.bundle.js:9578:10
    at forEach (http://angular.dev:8888/www/lib/js/ionic.bundle.js:7272:20)
    at HTMLAnchorElement.eventHandler (http://angular.dev:8888/www/lib/js/ionic.bundle.js:9577:5) 

这是什么原因造成的?

【问题讨论】:

    标签: angularjs broadcast


    【解决方案1】:

    我相信您需要使用 $ionicModal.fromTemplateUrl 方法创建一个新的模态对象,而不是重用旧的模态对象。删除模态对象后,角度绑定和 $scope 将被破坏,并且在您重新打开它时不可用。该错误来自模式尝试在已设置为 null 的 $scope 对象上调用广播。

    我怀疑你的代码应该是这样的(我不确定你什么时候必须调用“modal.show()”,所以可能需要移动它):

    .controller('openReviewCtrl', function($scope, $ionicModal) {
        $scope.master = {};
    
      function closeModal() {
        if ($scope.modal) {
          $scope.modal.remove();
          delete $scope.modal;
        }
      }
    
      $scope.openModal = function() {
        $ionicModal.fromTemplateUrl('templates/review.html', function(modal) {
          $scope.modal = modal;
          modal.show();
          }, {
            scope: $scope,
            animation: 'slide-in-up'
          });
      };
      $scope.closeModal = function() {
        closeModal();
      };
      //Be sure to cleanup the modal
      $scope.$on('$destroy', function() {
        closeModal();
      });
    
      $scope.writeReview = function(review) {
            console.log(review);
            closeModal();
      };
    })
    

    大概也可以将离子模态代码修复为可重用,但我没有看过这个。

    【讨论】:

    • 谢谢!这帮助我更清楚地了解发生了什么。这与一个小的编辑一起工作。模态对象方法是 modal.show() 而不是 modal.open()。一旦我得到它,它就会完美运行。打开一个新的模态窗口并可以发布数据。再次打开时,模态表单被重置。谢谢!
    • 哎呀,抱歉,open() 是一个错字,现在已修复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多