简体   繁体   English

AngularJS:使用ngRoute进行路由时未加载组件控制器

[英]AngularJS: Component controller not loaded when routing with ngRoute

I am an AngularJS beginner. 我是AngularJS初学者。 I have the following code: 我有以下代码:

A component defined by the following js file: 以下js文件定义的组件:

angular.module('EasyDocsUBBApp')
    .component('loginTag', {
        templateUrl: 'login-tag/login-tag.html',
        controller: function () {
            alert(1);
            this.login = function () {
                console.log(this.username + ':' + this.password);
            };
        }
    });

The content of my app.js file, where I also configured the routing is: 我还在其中配置路由的app.js文件的内容是:

var app = angular.module('EasyDocsUBBApp', ['ngRoute']);

app.config(function ($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'login-tag/login-tag.html'
        })
        .when('/test', {
            templateUrl: 'test.html'
        })
        .otherwise({
            redirectTo: 'login-tag/login-tag.html'
        });
});

My issue is that the controller is not loaded (the alert window does not appear). 我的问题是未加载控制器(没有出现警报窗口)。 Could someone indicate me what I did wrong? 有人可以告诉我我做错了什么吗? (if any supplementary details on my code are needed, please tell me) (如果需要我的代码的任何补充详细信息,请告诉我)

In your configuration for $routeProvider, try this: 在$ routeProvider的配置中,尝试以下操作:

.when('/', { template: '<login-tag></login-tag>' })

Remember to add your component.js to your index file. 请记住将component.js添加到索引文件中。

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

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