如何在AngularJS控制器中使用依赖项

时间:2015-08-12 07:41:12

标签: javascript angularjs dependency-injection

我正在制作一个MEANJS应用程序。我有一个AngularJS控制器,我想在其中使用Snoocore。

https://snoocore.readme.io/

'use strict';


angular.module('core').controller('HomeController', ['$scope', 'Authentication', 'snoocore',function($scope, Authentication, Snoocore) {
        // This provides Authentication context.
        $scope.authentication = Authentication;

        var reddit = new Snoocore({
            userAgent: 'test@documentation',
            oauth: {
                type: 'implicit', // required when using implicit OAuth
                mobile: true, // defaults to false.
                key: '', // Only requires the key! No secret needed.
                redirectUri: 'redirectUri set for your app',
                scope: [ 'read', 'flair', 'identity' ] // make sure to set all the scopes you need.
            }
        });
    }
]);

我通过Bower导入了Snoocore。它位于

public/lib/snoocore

控制器位于

public/modules/core/controllers/home.client.controller.js

现在我正在做的事情不起作用。我有点失落。我对Angular和MEANJS系统一般来说还是一个新手。

控制台输出以下内容

Error: [$injector:unpr] Unknown provider: snoocoreProvider <- snoocore
http://errors.angularjs.org/1.2.28/$injector/unpr?p0=snoocoreProvider%20%3C-<section data-ui-view="" class="ng-scope">noocore
    at http://localhost:3000/lib/angular/angular.js:78:12
    at http://localhost:3000/lib/angular/angular.js:3801:19
    at Object.getService [as get] (http://localhost:3000/lib/angular/angular.js:3929:39)
    at http://localhost:3000/lib/angular/angular.js:3806:45
    at getService (http://localhost:3000/lib/angular/angular.js:3929:39)
    at invoke (http://localhost:3000/lib/angular/angular.js:3956:13)
    at Object.instantiate (http://localhost:3000/lib/angular/angular.js:3976:23)
    at http://localhost:3000/lib/angular/angular.js:7315:28
    at chrome-extension://ighdmehidhipcmcojjgiloacoafjmpfk/dist/hint.js:1544:22
    at http://localhost:3000/lib/angular/angular.js:6711:34

3 个答案:

答案 0 :(得分:0)

我添加了标签

<script src="/lib/snoocore/dist/Snoocore-browser.min.js"></script>

到我的HTML头并将我的控制器更改为如下所示。

'use strict';


angular.module('core').controller('HomeController', ['$scope', 'Authentication' ,function($scope, Authentication) {
        // This provides Authentication context.
        $scope.authentication = Authentication;

        var reddit = new Snoocore({
            userAgent: 'test@documentation',
            oauth: {
                type: 'implicit', // required when using implicit OAuth
                mobile: true, // defaults to false.
                key: '', // Only requires the key! No secret needed.
                redirectUri: 'redirectUri set for your app',
                scope: [ 'read', 'flair', 'identity' ] // make sure to set all the scopes you need.
            }
        });

    }
]);

答案 1 :(得分:0)

如果您使用的是deicial应用程序库,则不能将依赖项作为普通的AngularJS模块注入,因为它没有AngularJS提供程序。

我认为你应该像普通的JS库一样使用库:

 var Snoocore = window.Snoocore;

'use strict';

angular.module('core').controller('HomeController', ['$scope', 'Authentication', function($scope, Authentication) {
        // This provides Authentication context.
        $scope.authentication = Authentication;

        var reddit = new Snoocore({
            userAgent: 'test@documentation',
            oauth: {
                type: 'implicit', // required when using implicit OAuth
                mobile: true, // defaults to false.
                key: '', // Only requires the key! No secret needed.
                redirectUri: 'redirectUri set for your app',
                scope: [ 'read', 'flair', 'identity' ] // make sure to set all the scopes you need.
            }
        });
    }
]);

试试这个,我希望能解决你的问题。

答案 2 :(得分:0)

Snoocore不是角度模块,因此您不能将其作为角度依赖项。创建角度服务/工厂以包装Snoocore或使用全局变量window.Snoocore。另外,请确保在HTML中包含Snoocore源文件。

相关问题