最好的方法是按照ccjmne的建议在其上使用管道
<div ng-show="movieRating.relevant | interpolate:this">
我已经详细说明了以下示例中的代码不要忘记在@NgModule 中导出和声明管道
html
<a [href]="this.$urlProjectDetails | interpolate:this" target="_blank">
自定义管道
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'interpolate' })
export class InterpolatePipe implements PipeTransform {
// {{ context.url | interpolate:this}}
transform(
text : string,
scope : object,
props : object = {} // OverWrite this
): string {
return this.interpolate(
text,
scope,
props
)
}
interpolate(stringToInterpolate : string, scope : object, props : object = null) {
var LOCAL = {
oSource : scope,// should be = this
value : null,
foundValue : true
}
return stringToInterpolate.replace(/\${(.*?)\}/g, function(match, expr : string) {
//try props for the value
LOCAL.value = props || {};
expr.split('.').some( currentExpr=>{
if(currentExpr in LOCAL.value){
LOCAL.value = LOCAL.value[currentExpr];
return false;
} else {
LOCAL.foundValue = false;
LOCAL.value = '';
return true;// stop execution of array with some()
}
})
//if we did not found a value in the given props, find it in the this scope
if(!LOCAL.foundValue){
LOCAL.value = LOCAL.oSource
expr.split('.').some( currentExpr=>{
if(currentExpr in LOCAL.value){
LOCAL.value = LOCAL.value[currentExpr];
return false;
} else {
LOCAL.foundValue = false;
LOCAL.value = '';
return true;// stop execution of array with some()
}
})
}
return LOCAL.value
});
};
}