【问题标题】:How is possible to get the bindings value in component controller with Angular 1.5 and Typescript?如何使用 Angular 1.5 和 Typescript 在组件控制器中获取绑定值?
【发布时间】:2016-10-28 17:57:30
【问题描述】:

我想创建一个左侧菜单组件,它根据通过绑定获得的字符串显示不同的菜单结构。

例如:

<left-hand-menu-component module='abc'></left-hand-menu-component>

然后它只显示 abc 模块的菜单结构。为了实现这一点,组件控制器必须能够通过绑定处理数据并进行必要的服务调用。

我已经在谷歌上搜索了一段时间如何实现这一点,我找到了 thisthis 帖子和 this 帖子。 Angular 文档中提供的示例太简单了。

我已经将上面的示例代码放在一起,当控制器可以处理通过绑定传入的值时,该部分就丢失了。

甚至有可能吗?或者它是一个指令而不是一个组件?

你知道它在哪里显示、博客条目或任何来源吗?

控制器构造函数中的两个console.log写出'undefined'作为结果。

组件:

module qa.gonogo {
    "use strict";

    export class LeftHandMenuComponent implements ng.IComponentOptions {


        public transclude: boolean = false;
        public controller: Function = LeftHandMenuComponentController;
        public controllerAs: string = "vm";
        public templateUrl: string = "app/content/lefthandmenu/leftHandMenuComponentTemplate.html";
        public bindings: any;

        constructor() {
            this.bindings = {
                module: '<'
            }
        }
    }

    angular
        .module("goNoGo")
        .component("gonogoLefthandmenuComponent", new LeftHandMenuComponent());

}

控制器:

export class LeftHandMenuComponentController implements ILeftHandMenuComponentController {

        menuStructure: IMenuStructure[];
        closeOthers: boolean = true;

        static $inject = [];

        constructor(public module: string) {
            console.log('binding', module);
            console.log('binding2', this.module);
        }

        public $onInit = (): void => {
            this.populateMenuStructure();
        }

路线。

$stateProvider
            .state("bfts",
                <ng.ui.IState>{
                    url: "/bfts",
                    views: <any>{
                        "leftHandMenu": <ng.ui.IState>{
                            template: "<gonogo-lefthandmenu-component module='bfts'></gonogo-lefthandmenu-component>"
                        },
                        "content": <ng.ui.IState>{
                            template: "bfts content"
                        }
                    }
                });

【问题讨论】:

  • 您是否尝试过在控制器类中使用绑定属性的名称定义属性?
  • 不清楚你面临什么问题。是的,这是可能的。只需将 http 调用移至服务即可。
  • 您链接的第一篇文章有​​一个示例,说明您在 FacebookIntegrationController 中需要设置属性。
  • @toskv:我尝试过这种方式,但没有成功。我已附上代码。

标签: javascript angularjs typescript


【解决方案1】:

绑定在控制器范围内可用,因此 this.module 必须工作。

我在 plnkr 中复制了您的代码,发现以下错误。

  1. 主要问题是您使用了“https://docs.angularjs.org/guide/component

    构造函数() { this.bindings = { 模块: '@' } }

  2. 不要注入模块变量。只需在您的控制器中定义它以满足打字稿编译器。

    公共模块:字符串;

  3. 您不需要控制器。模板会自动通过 $ctrl 获取作用域,请参阅我在 medium 处的示例。但不确定这是否很关键

  4. 正如我在帖子中所描述的,将 angular.module 调用放在源文件的底部总是一个好主意

修改代码:

angular.module('goNoGo', []);


class LeftHandMenuComponent implements ng.IComponentOptions {


    public transclude: boolean = false;
    public controller: Function = LeftHandMenuComponentController;
    //public controllerAs: string = "vm";
    public template: string = "<div>module :{{$ctrl.module}}</div>";
    public bindings: any;

    constructor() {
        this.bindings = {
            module: '@'
        }
    }
}



class LeftHandMenuComponentController {

  menuStructure: IMenuStructure[];
  closeOthers: boolean = true;
  public module: string;

  static $inject = [];

  constructor() {
      console.log('binding2', this.module);
  }

  //public $onInit = (): void => {
    //  this.populateMenuStructure();
}

angular
    .module("goNoGo")
    .component("gonogoLefthandmenuComponent", new LeftHandMenuComponent());

angular.element(document).ready(function () {
  angular.bootstrap(document, ['goNoGo']);
});

您可以在 plnkr http://plnkr.co/edit/TVI9l8 中玩耍

【讨论】:

  • 感谢您的帮助!对此,我真的非常感激!仅作记录:是的,@ 是我犯的错误之一。另一个是,控制器在一个单独的文件中。一旦我将它们放在同一个文件中,组件就开始按预期工作。我把controllerAs 留在那里,它也可以正常工作。我喜欢这种方式。
猜你喜欢
  • 2017-09-28
  • 2017-08-23
  • 2017-01-24
  • 2015-08-10
  • 2016-07-03
  • 2016-07-25
  • 2016-09-10
  • 1970-01-01
  • 2017-06-22
相关资源
最近更新 更多