简体   繁体   English

AngularJs指令不起作用/不可见

[英]AngularJs directive not working/visible

I'm just getting started with AngularJs. 我刚刚开始使用AngularJs。 And I'm trying to implement a login directive. 我正在尝试实现一个登录指令。 But I don't see the output? 但是我看不到输出吗? I've no errors in my console. 我的控制台没有错误。

My application structure: 我的应用程序结构:

在此处输入图片说明

( index.html is not visible) index.html不可见)

login.directive.js : login.directive.js

(function() {
    'use strict';

    angular
        .module('lnjapp.login',[])
        .directive('login', login);

    function login() {
        var directive = {
            template: '<p>test</p>',
            //restrict: 'E',
            Controller: LoginController,
            controllerAs: 'vm'
        };
        return directive;
    }   
})();

app.js : app.js

(function () {
    'use strict';

    angular.module('lnjapp', ['ngRoute', 'ngCookies', 'angular.filter','lnjapp.login','lnjapp.config'])
    .constant('GLOBALS', {
        url:'http://domain.dev/api/v1/'
    });

    $(document).ready(function() {
        $.material.init();
    });
})();

app/pages/login.html : app / pages / login.html

<login></login>

-- EDIT -- - 编辑 -

login.controller.js : login.controller.js

(function() {
    'use strict';

    angular.module('lnjapp.login',[])
    .controller('LoginController', LoginController);

function LoginController()
{}
})();

route-config.js : route-config.js

angular
    .module('lnjapp.config',[])
    .config(config);

function config($routeProvider) {
    $routeProvider
        .when('/', {
            templateUrl: '/app/pages/login.html'
        });
}

What am I doing wrong here? 我在这里做错了什么?

You are creating your lnjapp.login twice, once in login.directive.js and again in login.controller.js . 您将创建 lnjapp.login两次,一次是在login.directive.js ,一次是在login.controller.js The second time the module is created, it overwrites the first, and whatever was created in the first file will no longer be accessible. 第二次创建该模块时,它将覆盖第一个,并且将不再可访问在第一个文件中创建的任何内容。

You should always only create a module once, and get the already created module to add additional features in all other cases. 您应该始终只创建一个模块一次,并在所有其他情况下获取已创建的模块以添加其他功能。

Set (create): angular.module('lnjapp.login',[]) 设置(创建): angular.module('lnjapp.login',[])

Get (consume): angular.module('lnjapp.login') 获取(消耗): angular.module('lnjapp.login')

For more info and other best practices, see John Papa's excellent Angular Style Guide . 有关更多信息和其他最佳实践,请参阅John Papa出色的Angular Style Guide

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

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