AngularJs - 注入不按钮单击,抛出错误

时间:2014-08-11 09:30:40

标签: jquery angularjs

在我的应用程序中,我正在做路由,并注入一个方法来调用。当我点击按钮时我请求调用该方法。它不是给出结果,而是抛出错误。我想知道我注射的方式是正确还是错误!

任何人都帮我解决这个问题吗?

我的Js:

var locations = angular.module('location', ['ngRoute']);
locations.config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: 'HomeController',
            templateUrl: 'views/home.html'
        })
        .when('/inbox/:name', {
            controller: 'InboxController',
            templateUrl: 'views/inbox.html'
        })
        .when('/simpleInject',{
            controller:'injectController',
            templateUrl:'views/simpleInject.html'
        })
        .otherwise({redirectTo: '/'});
}]);
locations.controller('HomeController', ['$scope','$location', function($scope,$location){
    console.log('hi-HomeController', $location.url());
}]);

locations.controller('InboxController', ['$scope','$location', function($scope,$location){
    console.log('hi-InboxController',$location.url());
}]);
locations.factory('greeter', function(){
    return {
        greet : function(msg){
            alert(msg);
        }
    };
})
.controller('injectController', ['$scope', function($scope,greeter){
    $scope.sayHello = function(){
        greeter.greet('Hello!');
    }
}])

我的位置html文件(ng-view嵌套):我使用jade

doctype html
html(lang="en",ng-app='location')
    head
        title NG Totoal Referece Book Tutorials / Directives
        link(rel="stylesheet",href="http://cdn.jsdelivr.net/foundation/4.3.2/css/foundation.min.css")
        link(rel="stylesheet",href="styles/style.css")
    body
        header
            h1 Header
        div.content
            div(ng-view) //ng-view is here!
        footer
            h5 Footer

        script(src='js/lib/angular/angular.js')
        script(src='js/lib/angular/angular-route.js')
        script(src='js/lib/underscore/underscore-min.js')
        script(src='js/app.js')

我要加载的模板是:(simpleInject.html)

<button ng-click="sayHello()">Hello</button>

请问任何人纠正我吗? 提前谢谢!

1 个答案:

答案 0 :(得分:2)

控制器声明数组中缺少'greeter',它应该是这样的:

.controller('injectController', ['$scope', 'greeter', function ($scope, greeter) {

字符串序列(依赖项)应与控制器工厂的参数匹配。