簡體   English   中英

AngularJs-在按鈕單擊時注入不起作用,引發錯誤

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

在我的應用程序中,我正在做路由,並注入了要調用的方法。 當我單擊按鈕時,我要求調用該方法。 而不是給出結果,而是引發錯誤。 我想知道我在這里注入的方式是正確還是錯誤!

有人可以幫我解決這個問題嗎?

我的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嵌套):我使用玉

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>

有人糾正我嗎? 提前致謝!

控制器聲明的數組中缺少'greeter' ,應該是這樣的:

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

字符串順序(相關性)應與控制器出廠的參數匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM