简体   繁体   English

从@Component Angular 2更新@ Directive模板选择器中绑定的数据

[英]Updating data bound in @Directive's template selector from @Component Angular 2

I was looking around a particular thing in Angular 2 where I need to update the directive's selector's data bound variable with the upcoming changes. 我正在寻找Angular 2中的特定事物,我需要更新指令的选择器的数据绑定变量以及即将发生的更改。 It, however doesn't get updated on every change. 但是,每次更改都不会更新。

@Directive selector inside @Component's template, let's call comp.html @ Component模板中的@Directive选择器,让我们调用comp.html

<sunburst-chart [className]="'sunburst-chart'" width="200" height="200" bind-data="costData">
    </sunburst-chart>

The data I am targeting is ' costData ' 我定位的数据是' costData '

Here is how the directive looks like, 这是指令的样子,

 import { Directive, ElementRef, Attribute, SimpleChange, Input, OnChanges, ViewChild } from '@angular/core'; import * as d3 from 'd3'; @Directive({ selector: 'sunburst-chart' }) export class SunburstChart {} 

 import {Component, OnInit, ViewEncapsulation} from '@angular/core'; import {AWSTotalCostService} from '../services/index'; import {Router} from '@angular/router'; import {CostPricingTileModel} from '../models/costpricingtile.model'; @Component({ moduleId: module.id, selector: 'costing-module-tile', templateUrl: 'costpricingtile.html', providers: [AWSTotalCostService], encapsulation: ViewEncapsulation.None, styleUrls: ['costpricingtile.scss'], }) export class CostPricingTileComponent implements OnInit { private costData : any; 

This calls a service, that returns the data, after which, the view is not getting updated. 这将调用一个返回数据的服务,之后视图不会更新。

 private sample () : any { this.sampleService.sampleFun() .subscribe( data => { this.costData = data }, error => this.errorMessage = error ); } ngOnInit () : void { console.log(''); this.sample(); } 

Any help is much appreciated. 任何帮助深表感谢。

The problem I am facing is, I am not able to update the changes to the bound data in the html. 我面临的问题是,我无法更新html中绑定数据的更改。

PS. PS。 Using latest version of Angular ( The stable release, not beta versions ) 使用最新版本的Angular(稳定版本,而非beta版本)

Adding simpler terms, 添加更简单的术语,

Let's talk simple. 我们谈谈简单吧。 I have a d3 chart that I 've created as a directive (@Directive) with selector 'sunburst-chart'. 我有一个d3图表,我已经创建了一个带有选择器'sunburst-chart'的指令(@Directive)。 I have a component (@Component) that includes a templateUrl 'a html file', that has this selector as an element. 我有一个组件(@Component),其中包含一个templateUrl'一个html文件',它将此选择器作为一个元素。 I am pointing the data of that element to the component's data variable 'say costData'. 我将该元素的数据指向组件的数据变量'say costData'。 Initially it works fine as I have some data present, when the costData gets changed from service, the costData gets updated, but the changes are not bound back to the selector. 最初它工作正常,因为我有一些数据存在,当costData从服务更改时,costData得到更新,但更改没有绑定回选择器。 Hence the view change doesn't happen 因此,视图变化不会发生

You mentioned that you're using the latest Angular . 你提到你正在使用最新的Angular

Providers are no longer specified within the @Component decorator. 不再在@Component装饰器中指定提供者。 Instead, you ought to move them to @NgModule 相反,你应该将它们移动到@NgModule

There are three types of directives in Angular and yours doesn't seem to fit any one of them. Angular有三种类型的指令 ,而你的指令似乎不适合任何一种。

  • A component: directive with a template. 组件:带模板的指令。 Selector is an html tag such as your sunburst-chart , but it must use @Component decorator Selector是一个html标签,例如你的sunburst-chart ,但它必须使用@Component装饰器
  • An attribute directive. 属性指令。 Selector is an html attribute; 选择器是一个html属性; eg: [sunburst-chart] 例如: [sunburst-chart]
  • A structural directive, such as *ngIf , to change the structure of the DOM 结构指令,例如*ngIf ,用于更改DOM的结构

If you expect the selector to be an HTML tag, use a component instead of a directive. 如果您希望选择器是HTML标记,请使用组件而不是指令。

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

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