Angularjs Factory在Controller中作为依赖项添加时未加载

时间:2015-01-26 04:22:26

标签: javascript angularjs angular-services

我是Angularjs的新手,这是我第一次尝试使用它构建应用程序。我跟踪了index.html文件中加载的文件。

<!--Angularjs-->
<script src="assets/js/vendor/angular.min.js" type="text/javascript"></script>
<script src="assets/js/vendor/angular-route.min.js" type="text/javascript"></script>

<!--SleekDocketAppJS-->
<script src="app/app.module.js" type="text/javascript"></script>
<script src="app/app.route.js" type="text/javascript"></script>

<!--Controllers-->
<script src="app/components/dashboard/controller/dashboardController.js" type="text/javascript"></script>
<script src="app/components/product/controller/productController.js" type="text/javascript"></script>

他是我的app.module.js文件,我正在尝试在此处添加工厂名称 AuthFactory

'use strick';

var docket = angular.module("docket",['ngRoute']);

//Factory
docket.factory('AuthFactory',function(){

    console.log('auth factory');
    var authdata = {};
    authdata.name = "user 1";
    return (authdata);
});

//Running
docket.run(function(){
    console.log("runing...");
});

这是我的app.route.js文件,我根据路径调用控制器。

'use strict';

var docket = angular.module('docket');

docket.config(['$routeProvider',function($routeProvider){
    $routeProvider
    .when("/",
    {
            controller:"DashboardController",
            templateUrl: "app/components/dashboard/view/dashboard.html"
        })
        .when("/product",{
            controller:"productController",
            templateUrl: "app/components/product/view/product.html"
        })
        .otherwise({
            redirectTo:'/'
        });
}]);

我的简单dashboardController.js看起来像这样

var app = angular.module('docket');

    app.controller('DashboardController',['$scope, AuthFactory', function($scope, AuthFactory){

        console.log('dash controller');
        //console.log(AuthFactory);
    }]);

但是当我尝试使用加载服务DashboardController来运行应用程序时,我得到了他的错误。但是,如果我从控制器依赖项中删除该服务,则没有错误。 AuthFactory Error

1 个答案:

答案 0 :(得分:2)

这是$ scope

之后的拼写错误“'”
app.controller('DashboardController',['$scope, AuthFactory', function($scope, AuthFactory)

更改为

app.controller('DashboardController',['$scope', 'AuthFactory', function($scope, AuthFactory)