在angularjs中插入变量名为的字符串

在angularjs中插入变量名为的字符串,angularjs,Angularjs,如何在插值中插值 我正在使用一个写有消息字符串的文件,并使用{{vm.variable}},但我无法在HTML上显示带有变量值的字符串 输入字符串(JS):“欢迎来到这个世界。” 所需的输出字符串(HTML):“欢迎约翰·史密斯来到这个世界。” 看看下面我的代码 (函数(){ "严格使用",; 有棱角的 .module('应用程序',[]) .值(“”“MSG”{ “注册”:“欢迎来到这个世界。”, “登录”:“上次从{{location}登录” }).controller('MessageC

如何在插值中插值

我正在使用一个写有消息字符串的文件,并使用
{{vm.variable}}
,但我无法在HTML上显示带有变量值的字符串

输入字符串(JS):
“欢迎来到这个世界。”

所需的输出字符串(HTML):
“欢迎约翰·史密斯来到这个世界。”

看看下面我的代码

(函数(){
"严格使用",;
有棱角的
.module('应用程序',[])
.值(“”“MSG”{
“注册”:“欢迎来到这个世界。”,
“登录”:“上次从{{location}登录”
}).controller('MessageController',函数($scope、$parse、$interpolate、\u MSG\ux){
var vm=这个;
vm.name=“约翰·史密斯”;
vm.location=“印度”;
vm.welcomeMessage=\u MSG\u.signup;
//==========试验1=======
//vm.finalString=$interpolate(vm.welcomeMessage);
//除了
//第二次试验=====
//在{u MSG\u.signup中设置{vm.name}},然后执行以下操作
//vm.finalString=$parse(_MSG_).assign($scope,vm.name);
//错误[$parse:syntax]语法错误:标记“{”是表达式[Weclome{{{vm.name}.]第9列的意外标记,从[{vm.name}.]开始。
});
})();

{{vm.welcomeMessage}==>欢迎约翰·史密斯来到这个世界
//试验1
{{vm.finalString}

您需要将
$interpolate
绑定到
this\vm

比如:
$interpolate(vm.welcomeMessage)(this);

演示:

angular
.module('应用程序',[])
.value('MSG'{
“注册”:“欢迎来到这个世界。”,
“登录”:“上次从{{location}登录”
}).controller('MessageController',函数($scope,$parse,$interpolate,MSG){
var vm=这个;
vm.name=“约翰·史密斯”;
vm.location=“印度”;
vm.welcomeMessage=MSG.signup;
vm.finalString=$interpolate(vm.welcomeMessage)(this);
});

{{vm.welcomeMessage}}=>欢迎约翰·史密斯

//试验1
{{vm.finalString}
您需要将
$interpolate
绑定到
this\vm

比如:
$interpolate(vm.welcomeMessage)(this);

演示:

angular
.module('应用程序',[])
.value('MSG'{
“注册”:“欢迎来到这个世界。”,
“登录”:“上次从{{location}登录”
}).controller('MessageController',函数($scope,$parse,$interpolate,MSG){
var vm=这个;
vm.name=“约翰·史密斯”;
vm.location=“印度”;
vm.welcomeMessage=MSG.signup;
vm.finalString=$interpolate(vm.welcomeMessage)(this);
});

{{vm.welcomeMessage}}=>欢迎约翰·史密斯

//试验1
{{vm.finalString}
我忘了在HTML中添加一些代码以使其与angular兼容,但在编辑问题时不知道如何编辑代码部分。问题不清楚。需要更多解释。您希望看到什么?控制台中有错误吗?很抱歉。我需要完整的字符串;请参阅注释中提到的代码==>@lin yes之后的HTML部分d行,代码内我忘了在HTML中添加一些代码以使其角度兼容,但在编辑问题时不知道如何编辑代码部分。问题不清楚。需要更多解释。您期望什么?控制台中有错误吗?很抱歉。我需要完整的字符串;请参阅中提到的代码中HTML部分==>@lin是注释行,在codeit中,即使我尝试了
vm
an
this
作为第二个参数,也没有打印变量名。哦,这是我的错误,我也需要从MSG字符串值中删除
vm.
。Thanks@pro.mean:很高兴帮助您。:)即使我尝试了
vm
an
this
bo,它也不会打印变量名作为第二个参数哦,这是我的错误,我需要从MSG字符串值中删除
vm.
。Thanks@pro.mean:很高兴帮助你。:)