Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS:“;TypeError:无法调用方法'$广播';“无效”的定义;_Angularjs_Broadcast - Fatal编程技术网

AngularJS:“;TypeError:无法调用方法'$广播';“无效”的定义;

AngularJS:“;TypeError:无法调用方法'$广播';“无效”的定义;,angularjs,broadcast,Angularjs,Broadcast,我是AngularJS的新手,正在开发我的第一个移动应用程序,从Ionic框架开始。我有一个部分,让用户点击“写评论”按钮。这反过来又打开了一个离子模式,它有一个附加的形式。当用户填写表单并点击post时,我将对该数据进行处理,然后它调用的函数将破坏模式窗口。提交表单后,每次打开write review按钮时都会抛出错误,并且我有数据 打开模式文件的代码: <a ng-controller="openReviewCtrl" ng-click="openModal()" class="i

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

打开模式文件的代码:

  <a ng-controller="openReviewCtrl" ng-click="openModal()" class="item item-icon-left">
    <i class="icon ion-compose"></i>
    Write a Review (10 POINTS)
  </a>
点击“写评论”模式打开。
写入并提交数据。
记录写入的内容并关闭模式。
再次点击“写评论”会抛出错误
Post按钮重置页面,取消不起作用

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) 
TypeError:无法调用null的方法“$broadcast”
在ionic.views.Modal.inherit.show上(http://angular.dev:8888/www/lib/js/ionic.bundle.js:31554:26)
在作用域上。$Scope.openModal(http://angular.dev:8888/www/js/controllers.js:49:18)
在http://angular.dev:8888/www/lib/js/ionic.bundle.js:17182:21
在http://angular.dev:8888/www/lib/js/ionic.bundle.js:33774:11
在范围内。$eval(http://angular.dev:8888/www/lib/js/ionic.bundle.js:18939:28)
在范围内。$apply(http://angular.dev:8888/www/lib/js/ionic.bundle.js:19039:23)
在兰开夏。(http://angular.dev:8888/www/lib/js/ionic.bundle.js:33773:15)
在http://angular.dev:8888/www/lib/js/ionic.bundle.js:9578:10
在弗雷赫(http://angular.dev:8888/www/lib/js/ionic.bundle.js:7272:20)
在htmlanchorement.eventHandler(http://angular.dev:8888/www/lib/js/ionic.bundle.js:9577:5) 

是什么导致了这种情况?

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

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


大概也可以将ionic modal代码修复为可重用代码,但我还没有研究过这一点。

谢谢!这有助于我更清楚地了解正在发生的事情。这是一个小的编辑工作。模态对象方法是modal.show()而不是modal.open()。一旦我知道了这一点,它就能完美地工作。打开一个新的模式窗口,可以发布数据。再次打开时,模态形式被重置。非常感谢。对不起,open()是一个拼写错误,现已修复。
.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();
  };
})
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) 
.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();
  };
})