【问题标题】:Cannot read property '$on' of undefined无法读取未定义的属性“$on”
【发布时间】:2014-09-15 16:42:48
【问题描述】:

尝试在 AngularJS 中使用事件,但得到标题中所述的错误。

Followed this example 这看起来很简单,所以不太确定可能导致错误的原因。

我的代码:

.controller('FooterCtrl', [function($scope) {

    var showTime = false;

     $scope.$on('flip', function (event, args) {
        showTime = !showTime;
    });

}]);

.controller('View3Ctrl', [function($scope) { 

        $scope.$emit('flip', args);

}]);

有谁知道如何修复此错误及其含义?为什么 $scope 未定义?

【问题讨论】:

标签: javascript html angularjs angularjs-scope


【解决方案1】:
.controller('FooterCtrl', ["$scope",function($scope) {

    var showTime = false;

     $scope.$on('flip', function (event, args) {
        showTime = !showTime;
    });

}]);

.controller('View3Ctrl', ["$scope",function($scope) { 

    $scope.$emit('flip', args);

}]);

试试这个

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2018-12-17
    • 2016-06-30
    • 2016-05-09
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-08-05
    • 2016-07-18
    相关资源
    最近更新 更多