我有一个非常简单的问题.
在Javascript中,
"你好"+ function(){}
将打印 "hellofunction(){}"
因为Function.prototype将调用自己的toString方法,它将返回"function(){}"
现在,我想重写toString方法:
Function.prototype.toString = function(){
return "my" + SOME_PROPERTY + "output"
}
Run Code Online (Sandbox Code Playgroud)
在这个自定义方法中,我想function(){}
知道如何在toString方法中获取当前值,因为我无法toString再次执行,因为它将进行递归.
我希望最终输出为:
"myfunction(){}output"
Run Code Online (Sandbox Code Playgroud) 假设有一个组件ButtonComponent 和SideComponent。如何将“ButtonComponent”作为属性传递给“SideComponent”?
例如,
@Component({
selector: 'my-btn',
template: '<span>My button</span>'
})
export class ButtonComponent implements OnInit {
}
@Component({
selector: 'my-side',
template: '<div><div class="btn-section">{{myBtn}}</div><div class="content-section"><ng-content></ng-content></div></div>'
})
export class SideComponent implements OnInit {
@Input() myBtn:any = null;
}
Usage:
<my-side [myBtn]="btnTmpl">
Hello, this is my content
</my-side>
<my-btn #btnTmpl></my-btn>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,最终模板显示 [Object Object] 作为输出。