【问题标题】:Cannot inject directly into controller不能直接注入控制器
【发布时间】:2015-10-06 08:26:20
【问题描述】:

我正在尝试使用非常基本的代码在 mu 应用程序中设置 Angular,但它一直给我以下错误:

Uncaught ReferenceError: $scope is not defined

我拥有的是以下内容:

<!DOCTYPE html>
<html ng-app="test">

    <head>
        <!-- Other JS files here including jQuery!! -->
        <script src="~/Scripts/angular.js" type="text/javascript"></script>
    </head>

    <script src="~/Scripts/angular.js" type="text/javascript"></script>

</html>

app.js 我有:

var blaat11 = angular.module('test', []);

blaat11.controller('TestController', [$scope, function ($scope) {
    var aa = $scope.test;
}]);

但是我收到了我之前发布的错误。我想像这样将服务注入我的控制器。我以前做过这样的事情,从来没有遇到过任何麻烦。

有人知道为什么它不起作用吗?

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    $scope 括在引号中。这用于在缩小代码的情况下映射依赖注入。由于控制器外的$scope没有定义,所以抛出错误。

    blaat11.controller('TestController', ['$scope', function($scope) {
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-28
      • 2023-03-04
      • 1970-01-01
      • 2013-01-25
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-23
      相关资源
      最近更新 更多