Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs $parse(以角度表示)(2/4)_Angularjs_Angular - Fatal编程技术网

Angularjs $parse(以角度表示)(2/4)

Angularjs $parse(以角度表示)(2/4),angularjs,angular,Angularjs,Angular,我有一个AngularJS组件,它接受一个字符串模板,我像这样编译它,然后用它来呈现各种项目: this.$parse(template); 我想在角度分量(v.4.0.0-rc.3)中做一些事情,但我很难找到解决方案。我发现了一些例子;然而,它们都来自Angular 2 beta版,似乎不起作用() 到目前为止,我已将编译器服务添加到我的模块中: import { COMPILER_PROVIDERS } from '@angular/compiler'; ... providers: [

我有一个AngularJS组件,它接受一个字符串模板,我像这样编译它,然后用它来呈现各种项目:

this.$parse(template);
我想在角度分量(v.4.0.0-rc.3)中做一些事情,但我很难找到解决方案。我发现了一些例子;然而,它们都来自Angular 2 beta版,似乎不起作用()

到目前为止,我已将编译器服务添加到我的模块中:

import { COMPILER_PROVIDERS } from '@angular/compiler';
...
providers: [
        COMPILER_PROVIDERS,
   ]
我正在拉入并调用解析器服务:

constructor(private parser: Parser) { }
...

var ast = this.parser.parseInterpolation(template, null);
这一点我有点迷路了。在杂草中挖掘这是我能找到的唯一关于那种类型的参考资料


我走对了吗?我觉得应该有一个简单的解决方案来插值给定的模板和上下文到字符串

我的理由是我想以标准的“如何根据路线设置窗口标题”为例,使其更具动态性

由此:

{
        path: 'users',
        component: UserComponent,
        resolve: { user: UserResolver },
        data: {title: 'User'}
}
为此:

{
        path: 'users',
        component: UserComponent,
        resolve: { user: UserResolver },
        data: {title: 'User {{user.name}}'}
}
由于我完全可以控制所发生的事情,所以插值可能有些过分,我只是更新了解析逻辑,以了解title属性可能是字符串或函数,以及如果它是函数该怎么办:

if (snapshot.data['title']) {
    let title = snapshot.data['title'] as (string | Function);
    if (typeof title === 'function') {
        titleParts.push(title(snapshot.data));
    } else {
        titleParts.push(title);
    }
}
当路线变为:

{
    path: 'users',
    component: UserComponent,
    resolve: { user: UserResolver },
    data: {title: (ctx: any) => `User ${(ctx.user as User).Name}`}
}

这并不是每种情况下的解决方案,但我认为如果其他人也尝试做类似的事情,这可能会有所帮助。

您能展示您的解决方案的任何实施细节吗?这似乎不是答案,而是问题本身的附加细节。