简体   繁体   English

Angular 1.5 组件 - 从指令转换后未呈现模板

[英]Angular 1.5 component - template not rendered after converting from directive

I have an Angular directive ( myUser ) which is working.我有一个正在运行的 Angular 指令( myUser )。 It's defined in an AngularJS 1.5 project, using TypeScript like this (I'm assigning the controller directly inside the directive) .它是在 AngularJS 1.5 项目中定义的,像这样使用 TypeScript(我直接在指令内部分配控制器)。 The directive ( myUser ) is being linked to it's parent module (you can see this at the bottom of the code sample below - you need to scroll)指令( myUser )正在链接到它的父模块(您可以在下面的代码示例底部看到这一点 - 您需要滚动)

class myUser {

    controller: any;
    templateUrl: string;
    controllerAs: string;

    constructor() {
        this.controller = myCtrl;
        this.controllerAs = 'ctrl';
        this.templateUrl    = '/mytemplate.html';
    }
    static userFactory() {
        var instance = new myUser();
        return instance;
    }
}


class myCtrl {

    global: any;
    mySrv: any;
    username: any;
    focus: boolean;

    static $inject = ['MyDependency', 'MyService'];

    constructor(UserInfo, mySrv) {
        this.global     = UserInfo;
        this.mySrv    = mySrv;
        this.myData   = this.global ? this.global.myData : false;
        this.focus      = false;
    }

    check() {
        if(this.myData){
            return true;
        };
        return false;
    }

    signIn(model) {
        this.mySrv.signIn(model);
    }

    signOut() {
        this.mySrv.signOut();
    }
}

class myOtherDir {
     ... other directive definition
}

class myOtherCtrl {
     ... other controller definition
}

angular
    .module('shared.myNavigation', [])
    .directive('myUser', myUser.userFactory)
    .directive('myOtherDir', myOtherDir.dirFactory)
    .controller('myOtherCtrl', myOtherCtrl)

When I run the app, the directive is replaced by the content of /mytemplate.html:当我运行应用程序时,指令被 /mytemplate.html 的内容替换:

html ... <my-user></my-user> => is replaced by template markup html ... <my-user></my-user> => 被模板标记替换

When I change the directive to a component though, using the Angular 1.5 component API like this, the component declaration in my HTML is no longer replaced by the template markup:但是,当我将指令更改为组件时,使用像这样的 Angular 1.5 组件 API,我的 HTML 中的组件声明不再被模板标记替换:

angular
    .module('shared.myNavigation', [])
    .component('myUser', myUser.userFactory)
    .directive('myOtherDir', myOtherDir.dirFactory)

There are no errors, and the only thing that's changed is that I'm now linking this to the module as a component instead of a directive.没有错误,唯一改变的是我现在将它作为组件而不是指令链接到模块。 I've gone through my code and I believe it should be fully compatible with the component API.我已经浏览了我的代码,我相信它应该与组件 API 完全兼容。 Is there something obvious I'm missing?有什么明显的我遗漏了吗? I've Googled just about every similar issue I can find, and I'm not sure where I'm going wrong.我在谷歌上搜索了几乎所有我能找到的类似问题,但我不确定我哪里出错了。

I'm not completely sure why, but instead of calling the static userFactory function to create a class instance, I needed to directly create the instance when hooking up the component, like this:我不完全确定为什么,但不是调用静态 userFactory 函数来创建类实例,而是需要在连接组件时直接创建实例,如下所示:

angular
    .module('shared.myNavigation', [])
    .component('myUser', new myUser())

Bonus points if anyone can explain why this is required.如果有人可以解释为什么需要这样做,则加分。

Sorry for resurrecting this but I just came up with this exact problem on my project and solved without classes.很抱歉让这个问题复活,但我刚刚在我的项目中提出了这个确切的问题,并在没有课程的情况下解决了。

It seems the component registration functions takes a function expression and not a function reference as in the directive registration.似乎组件注册函数采用函数表达式而不是指令注册中的函数引用 So in the source provided with the question just adding parenthesis to registration will execute and register the component.因此,在随问题提供的源代码中,只需在注册中添加括号即可执行并注册组件。

I just did a directive to component conversion (no classes just regular javascript) and it was fixed.我只是做了一个组件转换指令(没有类只是普通的javascript)并且它被修复了。

angular
.module('shared.myNavigation', [])
.component('myUser', myUser.userFactory()) /*Converting to expression*/
.directive('myOtherDir', myOtherDir.dirFactory)

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

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