【问题标题】:Why $onInit method is not invoked in the example below?为什么在下面的示例中没有调用 $onInit 方法?
【发布时间】:2016-03-31 19:19:42
【问题描述】:

我想使用 angular 的组件方法,但似乎有问题。我一直在仔细检查这段代码。没有错字,它似乎适合文档,但仍然无法正常工作。

我有checked,已安装 Angular 1.5.3。

控制台上没有输出。根据documentationthis 博客条目,我应该看到“onInit”文本。

组件的模板显示正确,我可以看到模板已加载,但控制器似乎没有实例化/触发。

我的应用是用 Typescript 编写的。

组件

module sayusiando.dilib.spa {

    export class LeftHandMenuComponent implements ng.IComponentOptions {

        public transclude: boolean = false;
        public controller: Function = LeftHandMenuController;
        public controllerAs: string = "vm";
        public templateUrl: string = "app/layout/leftHandMenu/leftHandMenuTemplate.html";

    }

    angular
        .module("dilib")
        .component("dilibLeftHandMenu", new LeftHandMenuComponent());
}

编译后的代码

var sayusiando;
(function (sayusiando) {
    var dilib;
    (function (dilib) {
        var spa;
        (function (spa) {
            var LeftHandMenuComponent = (function () {
                function LeftHandMenuComponent() {
                    this.transclude = false;
                    this.controller = spa.LeftHandMenuController;
                    this.controllerAs = "vm";
                    this.templateUrl = "app/layout/leftHandMenu/leftHandMenuTemplate.html";
                }
                return LeftHandMenuComponent;
            })();
            spa.LeftHandMenuComponent = LeftHandMenuComponent;
            angular
                .module("dilib")
                .component("dilibLeftHandMenu", new LeftHandMenuComponent());
        })(spa = dilib.spa || (dilib.spa = {}));
    })(dilib = sayusiando.dilib || (sayusiando.dilib = {}));
})(sayusiando || (sayusiando = {}));

布局模板

<div>
    <dilib-left-hand-menu class="col-lg-2"></dilib-left-hand-menu>
</div>

LeftHandMenuController

module sayusiando.dilib.spa {
    "use strict";

    export interface ILeftHandMenuController {
    }


    export class LeftHandMenuController implements ILeftHandMenuController {

        $onInit: Function = (() => {console.log("onInit");});


        static $inject = ["LeftHandMenuService"];
        constructor(leftHandMenuService: sayusiando.dilib.spa.ILeftHandMenuService) {

            console.log("con");
            this.leftHandMenuService = leftHandMenuService;

            //this.activate();
            console.log("construct");
        }

        activate() { //activate logic }

    }

    angular
        .module('dilib')
        .controller('leftHandMenuController', LeftHandMenuController);

}

编译的控制器代码

var sayusiando;
(function (sayusiando) {
    var dilib;
    (function (dilib) {
        var spa;
        (function (spa) {
            "use strict";
            var LeftHandMenuController = (function () {
                function LeftHandMenuController(leftHandMenuService) {
                    this.$onInit = (function () { console.log("onInit"); });
                    console.log("con");
                    this.leftHandMenuService = leftHandMenuService;
                    //this.activate();
                    console.log("construct");
                }
                LeftHandMenuController.prototype.activate = function () {
                    var _this = this;
                    this.leftHandMenuService.getLeftHandMenu()
                        .then(function (result) {
                        _this.leftHandMenu = result;
                    });
                };
                LeftHandMenuController.$inject = ["LeftHandMenuService"];
                return LeftHandMenuController;
            })();
            spa.LeftHandMenuController = LeftHandMenuController;
            angular
                .module('dilib')
                .controller('leftHandMenuController', LeftHandMenuController);
        })(spa = dilib.spa || (dilib.spa = {}));
    })(dilib = sayusiando.dilib || (sayusiando.dilib = {}));
})(sayusiando || (sayusiando = {}));

【问题讨论】:

    标签: javascript angularjs typescript


    【解决方案1】:

    我以错误的方式调用了 $oninit。这是正确的、运行良好的代码:

    module sayusiando.dilib.spa {
        "use strict";
    
        export interface ILeftHandMenuControllerScope {
    
        }
    
    
        export class LeftHandMenuController implements ILeftHandMenuControllerScope {
    
            public leftHandMenu: Array<sayusiando.dilib.spa.IModuleContract>;
    
            static $inject = ["leftHandMenuService"];
            constructor(
                private leftHandMenuService: sayusiando.dilib.spa.ILeftHandMenuService) {
    
            }
    
    
            public $onInit = () => {
    
                this.leftHandMenuService.getLeftHandMenu()
                    .then((result: Array<sayusiando.dilib.spa.IModuleContract>): void => {
                        this.leftHandMenu = result;
                    });
    
            }
    
        }
    
        angular
            .module('dilib')
            .controller('leftHandMenuController', LeftHandMenuController);
    
    }
    

    【讨论】:

      【解决方案2】:

      我认为这是由于模块定义中缺少依赖参数列表。这两种说法是有区别的:

      angular.module("dilib")
      angular.module("dilib",[])
      

      第一个语句尝试访问名为 dilib 的现有模块,而第二个语句尝试创建一个没有依赖关系的模块 dilib。我相信您正在尝试创建一个新模块,因此需要第二种格式。

      【讨论】:

      • 感谢您的回答!我不认为这是问题所在。这个组件是一个更大的应用程序的一部分,我也使用其他组件。我没有遇到这个问题的原因是,我没有使用已经存在的组件的控制器。到目前为止,组件构建了应用程序的布局。
      • 另外,我没有看到布局模板中使用了 ng-controller 指令。您是否将其与其他地方的模板相关联?
      • 我浏览了文档,没有直接调用控制器的示例。
      • 有。在docs.angularjs.org/guide/component,搜索文本 ng-controller。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2019-01-23
      • 2015-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多