Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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 语法:如何在服务中侦听或发出$emit?_Angularjs - Fatal编程技术网

Angularjs 语法:如何在服务中侦听或发出$emit?

Angularjs 语法:如何在服务中侦听或发出$emit?,angularjs,Angularjs,我想在rootScope上侦听'MyCommittedEvent',但此代码有问题: app.service('MyService',function($rootScope){ $rootScope.$on('myEmittedEvent', function(){ <===== error here // code goes here }); }); app.service('MyService',函数($rootScope

我想在rootScope上侦听'MyCommittedEvent',但此代码有问题:

app.service('MyService',function($rootScope){     
      $rootScope.$on('myEmittedEvent', function(){    <===== error here
           // code goes here
      });
});
app.service('MyService',函数($rootScope){
$rootScope.$on('myEmittedEvent',function(){请重试

app.service('MyService',['$rootScope',function($rootScope){     
  $rootScope.$on('myEmittedEvent', function(){    <===== error here
       // code goes here
  });

  return {};

}]);
app.service('MyService',['$rootScope',function($rootScope){

$rootScope.$on('myEmittedEvent',function(){看起来有效…唯一的
app.service
-是
app
声明的吗?这个代码之前有什么东西吗?我看到的只是一个额外的
}
,看起来有效。尝试删除最后一个
}
之后的分号。我的意思是
app.service('MyService',function>(){};
你能在plunker中复制这个吗?你确定你复制/粘贴的代码片段正确吗?(我的意思是,我们应该找到一个语法错误,并且已经做了两次复制/粘贴错误。)听起来你可能在某处拼写错了
函数
。。。