简体   繁体   English

AngularJs-在按钮单击时注入不起作用,引发错误

[英]AngularJs - Inject not working on button click, throws the error

In my app, I am doing route, and injecting a method to call. 在我的应用程序中,我正在做路由,并注入了要调用的方法。 when i click on button I requesting to call the method. 当我单击按钮时,我要求调用该方法。 instead of giving the result, it throws the error. 而不是给出结果,而是引发错误。 I wondering the way which i injecting is correct or wrong here! 我想知道我在这里注入的方式是正确还是错误!

any one help me to sort this please? 有人可以帮我解决这个问题吗?

my Js: 我的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!');
    }
}])

my location html file (where the ng-view nest): I use jade 我的位置html文件(其中ng-view嵌套):我使用玉

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')

the template I am loading in to view is: (simpleInject.html) 我正在加载以查看的模板是:(simpleInject.html)

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

any one correct me please? 有人纠正我吗? Thanks in advance! 提前致谢!

The 'greeter' is missing in the array of the controller declaration, it should be like this: 控制器声明的数组中缺少'greeter' ,应该是这样的:

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

The sequence of string (dependencies), should match the parameters of the controller factory. 字符串顺序(相关性)应与控制器出厂的参数匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM