我刚刚开始研究一个Angular项目但是有点卡住了.我有一个组件从(可信的)外部源接收HTML字符串.我想从这个字符串中提取和更新值,并在我的组件中显示它.
现在可以使用以下代码轻松完成字符串的显示:
<div [innerHTML]="displayString"></div>
Run Code Online (Sandbox Code Playgroud)
我的输入字符串有一些<span>元素,data-card-text元素作为标识符附加.是否可以提取此值,并在我的组件需要时更新它?
例:
let displayString:string = "<svg><text data-card-text>Value I want to update</text></svg>"
在Angular 2中,甚至可以进行这样的操作吗?
我有两个组件,一个父母和一个孩子,我想从孩子使用DI访问父组件中的变量,但我没有得到那个.我做了一个plunker演示http://plnkr.co/edit/fcfweLJAmadKVWKXF25l?p=preview ...我试图从child访问变量的值并且正确地得到但不是从父到子.这就是我在子组件中访问变量的方式
@Component({
selector: 'child',
providers:[AppComponent]
template: '<h1>My second Angular 2 App</h1>'
})
class NameComponent {
name:string;
constructor(AppComponent: AppComponent) {
this.name = AppComponent.getName();
console.log(this.name);
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以使用DI从父级访问变量?请告诉我如何获取价值观
我想app.component.ts从其他页面(otherpage.ts)或其他组件访问和更改变量或访问方法,如;
app.component.ts
@Component({
templateUrl: 'app.html'
})
export class MyApp {
accessedVariable: any;
constructor(){ }
accessedMethod() {
..something
}
}
Run Code Online (Sandbox Code Playgroud)
otherpage.ts
@Component({
selector: 'page-other',
templateUrl: './otherpage.html',
})
export class OtherPage {
constructor() { }
}
Run Code Online (Sandbox Code Playgroud)