Angular.js如何将$ scope注入$ scope.on()的指令()?

时间:2022-11-13 19:38:10

for the directive I wrote, how do I get $scope in there so I can do $scope.$on()? I could inject $rootScope, but I want to use $scope to catch the broadcast.

对于我写的指令,如何在那里获得$ scope所以我可以做$ scope。$ on()?我可以注入$ rootScope,但我想使用$ scope来捕获广播。

angular.module('monitorApp')
.run(function($rootScope, $interval) {
            $interval(function(){
                $rootScope.$broadcast('ONE_SEC');
            }, 1000);
});

angular.module('monitorApp')
.directive("countDown", [ 'sseHandler', function (sseHandler) {
    console.log(sseHandler.broadcastStamp.cpuResult);
    return {
        scope: {
            countFrom: "="
        },
        link: function(scope, $scope){
           scope.countFrom = sseHandler.broadcastStamp.cpuResult;
           $scope.$on('ONE_SEC', function(scope) {
               sseHandler.broadcastStamp.cpuResult--;
           });
        }
    }
}]);

UPDATE: with scope injection, I get error: Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope <- countDownDirective

更新:使用范围注入,我收到错误:错误:[$ injector:unpr]未知提供者:$ scopeProvider < - $ scope < - countDownDirective

angular.module('monitorApp')
.directive("countDown", [ 'sseHandler', '$scope', function (sseHandler, $scope) {
    console.log(sseHandler.broadcastStamp.cpuResult);
    return {
        scope: {
            countFrom: "="
        },
        link: function(scope, $scope){
           scope.countFrom = sseHandler.broadcastStamp.cpuResult;
           $scope.$on('ONE_SEC', function(scope, $scope) {
               sseHandler.broadcastStamp.cpuResult--;
           });
        }
    }
}]);

2 个解决方案

#1


5  

You can just use scope.$on, you don't need to try and inject $scope:

你可以只使用scope。$ on,你不需要尝试注入$ scope:

scope.$on('ONE_SEC', function() {
           //do stuff
});  

Example: http://jsfiddle.net/G2r7G/

#2


0  

The link gives arguments scope, element and attributes in that order. In your link you are actually assigning element to $scope

该链接以该顺序提供参数范围,元素和属性。在您的链接中,您实际上是将元素分配给$ scope

To inject the $scope you inject it at the same place you're injecting ssehandler

要注入$ scope,你将它注入你注入ssehandler的同一个地方

directive("countDown", [ 'sseHandler', '$scope', function (sseHandler, $scope)....

指令(“countDown”,['sseHandler','$ scope',函数(sseHandler,$ scope)....

#1


5  

You can just use scope.$on, you don't need to try and inject $scope:

你可以只使用scope。$ on,你不需要尝试注入$ scope:

scope.$on('ONE_SEC', function() {
           //do stuff
});  

Example: http://jsfiddle.net/G2r7G/

#2


0  

The link gives arguments scope, element and attributes in that order. In your link you are actually assigning element to $scope

该链接以该顺序提供参数范围,元素和属性。在您的链接中,您实际上是将元素分配给$ scope

To inject the $scope you inject it at the same place you're injecting ssehandler

要注入$ scope,你将它注入你注入ssehandler的同一个地方

directive("countDown", [ 'sseHandler', '$scope', function (sseHandler, $scope)....

指令(“countDown”,['sseHandler','$ scope',函数(sseHandler,$ scope)....