如何在Angular 1.5和Typescript中获取组件控制器中的绑定值?
How is possible to get the bindings value in component controller with Angular 1.5 and Typescript?
我想创建一个左侧菜单组件,根据它通过绑定获得的字符串显示不同的菜单结构。
例如:<left-hand-menu-component module='abc'></left-hand-menu-component>
然后只显示abc模块的菜单结构。为了实现这一点,组件控制器必须能够处理通过绑定来的数据,并进行必要的服务调用。
我一直在谷歌周围一段时间如何实现这一点,我发现这和这篇文章和这篇文章。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"
}
}
});
绑定在控制器范围内可用,所以这个。
我在plnkr中复制了你的代码,发现了以下错误。
-
主要问题是您使用了"<"绑定,这是一种单向绑定。因此,它尝试访问"btfs"变量,该变量在您的作用域中未定义,因此未定义是绝对正确的输出。要使用字符串,请使用"@",参见https://docs.angularjs.org/guide/component
构造函数(){这一点。绑定= {模块:"@"}}
-
不要注入模块变量,只需在控制器中定义它以满足typescript编译器。
-
你不需要控制器。模板通过$ctrl自动获得注入的作用域,参见我在medium中的例子。但不确定这是否关键
-
正如我在我的帖子中所描述的,放置angular总是一个好主意。源文件
底部的模块调用
修改代码:
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玩
相关文章:
- 如何在另一个组件中获取指令/组件实例
- Vue.js获取组件中的一个元素
- 从子级获取React dom组件
- React - 从 DOM 元素获取组件以进行调试
- 如何获取子组件的内容Angular 2中的子组件
- 在 ExtJS 中获取父组件
- 如何创建一个组件,我可以设置在 Angular 2 中的属性中获取 URL 数据
- 从子窗体组件ReactJS中获取道具
- Vue.js从另一个组件获取数据
- 如何获取当前Vue组件的DOM元素
- React Native:将id设置为一个组件并获取该组件's的id
- 如何在Vue.js中获取组件元素的offsetHeight
- 在react组件中使用ajax获取实时更新的数据
- 从淘汰js组件中获取数据
- 反应:从父组件获取子组件数据
- React Redux 子组件获取父组件设置的全局属性,而无需通过 props 传递它
- 如何将数据从子组件获取到 props 中传递的按钮的 onclick 中
- 从其他组件获取参数[React JS]
- 可以't从子组件获取要在父组件上执行的方法
- 组件获取重复的内容,而不是刷新