【发布时间】:2016-10-28 17:57:30
【问题描述】:
我想创建一个左侧菜单组件,它根据通过绑定获得的字符串显示不同的菜单结构。
例如:
<left-hand-menu-component module='abc'></left-hand-menu-component>
然后它只显示 abc 模块的菜单结构。为了实现这一点,组件控制器必须能够通过绑定处理数据并进行必要的服务调用。
我已经在谷歌上搜索了一段时间如何实现这一点,我找到了 this 和 this 帖子和 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