AngularJS-控制器通信

AngularJS-控制器通信,angularjs,controller,Angularjs,Controller,在我的网站上,我有以下结构 收割台\u控制器: myApp.controller('Header_controller',function($scope,$rootScope,$location,Genral_model){ $scope.logout = function(){ var data = {}; $rootScope.$emit('logout_clicked', data); // NOT WORKING $rootSc

在我的网站上,我有以下结构

收割台\u控制器

myApp.controller('Header_controller',function($scope,$rootScope,$location,Genral_model){

    $scope.logout = function(){
        var data = {};
        $rootScope.$emit('logout_clicked', data);  // NOT WORKING
        $rootScope.$broadcast('logout_clicked', data); // NOT WORKING
    }

});
用户/控制器:

myApp.controller('Users_controller', function ($scope,$rootScope,Users_model,$location,$state) {

    $rootScope.$on('logout_clicked', function(event, resp) {
        Users_model.unRegisterCookies();
    });
})
用户和用户模型:

myApp.factory('Users_model', function($rootScope,$http,$cookies) {
  var factory={};

  factory.unRegisterCookies = function(){
      alert('log out');
  }


  return factory;
});
我试图通过将广播从
Header\u controller
发送到
Users\u controller
来调用
unRegisterCookies
函数,但它不适用于发射,也不适用于广播


如何使其工作?

这是您正在尝试的基本代码,它对我有效。也许你的代码中的其他地方有错误

html:


这和你正在尝试的基本代码是一样的,它对我有效。也许你的代码中的其他地方有错误

html:


这和你正在尝试的基本代码是一样的,它对我有效。也许你的代码中的其他地方有错误

html:


这和你正在尝试的基本代码是一样的,它对我有效。也许你的代码中的其他地方有错误

html:


有关代码的工作版本,请参阅此plnkr:

这与您发布的代码完全相同,并且它正在工作。您的页面中可能还有其他错误。如果您使用的是Chrome或FireFox,请打开控制台,查看您的页面显示了哪些错误

html:


有关代码的工作版本,请参阅此plnkr:

这与您发布的代码完全相同,并且它正在工作。您的页面中可能还有其他错误。如果您使用的是Chrome或FireFox,请打开控制台,查看您的页面显示了哪些错误

html:


有关代码的工作版本,请参阅此plnkr:

这与您发布的代码完全相同,并且它正在工作。您的页面中可能还有其他错误。如果您使用的是Chrome或FireFox,请打开控制台,查看您的页面显示了哪些错误

html:


有关代码的工作版本,请参阅此plnkr:

这与您发布的代码完全相同,并且它正在工作。您的页面中可能还有其他错误。如果您使用的是Chrome或FireFox,请打开控制台,查看您的页面显示了哪些错误

html:


能否尝试将警报(“嘿!”)添加到$scope.logout()函数中,以确保调用该函数。如果可行,请尝试在“$rootScope.$on('logout_clicked',function(event,resp){”中放置警报,以确保调用该警报。logout被调用,但$rootScope。$on('logout_clicked',function(event,resp){”无效。您可以尝试在$scope.logout()中添加警报(“嘿!”)函数以确保调用该函数。如果这样做有效,请尝试在“$rootScope.$on('logout_clicked',function(event,resp){”中放置警报,以确保调用该函数。logout被调用,但$rootScope。$on('logout_clicked',function(event,resp)无效。是否可以尝试在$scope.logout()中添加警报(“嘿!”)函数以确保调用该函数。如果这样做有效,请尝试在“$rootScope.$on('logout_clicked',function(event,resp){”中放置警报,以确保调用该函数。logout被调用,但$rootScope。$on('logout_clicked',function(event,resp)无效。是否可以尝试在$scope.logout()中添加警报(“嘿!”)函数以确保调用该函数。如果有效,请尝试在“$rootScope.$on('logout_clicked',function(event,resp){”中放置警报以确保调用该函数。logout被调用,但$rootScope。$on('logout_clicked',function(event,resp)不起作用。也许您可以将代码放在Plnkr.co或其他地方,以便我们可以看到它(不起作用)正在工作。也许你可以把代码放在Plnkr.co或其他东西上,这样我们就可以看到它(不)工作。也许你可以把代码放在Plnkr.co或其他东西上,这样我们就可以看到它(不)工作。也许你可以把代码放在Plnkr.co或其他东西上,这样我们就可以看到它(不)工作。
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.0.x" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div ng-controller="HeaderCtrl">
      <p>Button has been hit {{counter}} times.</p>
    </div>
    <div ng-controller="UsersCtrl">
      <button ng-click="pushme()">Count Me In</button>
    </div>

  </body>
</html>
var app = angular.module('plunker', []);

app.controller('HeaderCtrl', function($scope, $rootScope) {
  $scope.counter = 0;

  $rootScope.$on('countMeIn', function (event, data) {
    $scope.counter = $scope.counter + 1;
  });

});


app.controller('UsersCtrl', function($scope, $rootScope) {

  $scope.pushme = function() {
    $rootScope.$emit("countMeIn", {});
  };

});
<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.0.x" src="https://code.angularjs.org/1.2.28/angular.js" data-semver="1.2.28"></script>
    <script data-require="angular.js@1.0.x" src="https://code.angularjs.org/1.2.28/angular-cookies.js" data-semver="1.2.28"></script>
    <script src="app.js"></script>
  </head>

  <body>
    <div ng-controller="HeaderCtrl">
      <h1>Headers Controller</h1>
      <button ng-click="logout()">Log out</button>
    </div>
    <div ng-controller="UsersCtrl">
      <h1>Users Controller</h1>
    </div>

  </body>
</html>
var myApp = angular.module('myApp', ['ngCookies']);

myApp.controller('HeaderCtrl', function($scope, $rootScope) {

  $scope.logout = function(){
      var data = {};
      $rootScope.$emit('logout_clicked', data);  // NOT WORKING
      $rootScope.$broadcast('logout_clicked', data); // NOT WORKING
  }  

});

myApp.controller('UsersCtrl', function($scope, $rootScope, Users_model) {

  $rootScope.$on('logout_clicked', function(event, resp) {
      Users_model.unRegisterCookies();
  });

});

myApp.factory('Users_model', function($rootScope,$http,$cookies) {
  var factory={};

  factory.unRegisterCookies = function(){
      alert('log out');
  }


  return factory;
});