尾随AngularJS前pressions双引号 [英] Trailing double quotes in AngularJS expressions

查看:150
本文介绍了尾随AngularJS前pressions双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个<一个href=\"http://stackoverflow.com/questions/16796341/set-angular-scope-variable-in-markup/28432472#28432472\">highly upvoted回答(+50)和<一个href=\"http://stackoverflow.com/questions/16665319/how-to-set-a-variable-inside-of-a-template#comment54563800_16665319\">this评论暗示结束前的pression用分号和双引号,;,可使用时要定义模板中的一个角度变量勿使它作为文本。

我测试过和它的作品。例如:

\r
\r

&LT;脚本SRC =htt​​ps://ajax.googleapis.com/ajax /libs/angularjs/1.2.23/angular.min.js\"></script>\r
\r
&LT; D​​IV NG-应用=&GT;\r
  &LT; P&gt;这变量被渲染:{{文本1 ='是'}}&LT; / P&GT;\r
  &LT; P&gt;这变量是不是:{{文本2 ='是';}}&LT; / P&GT;\r
&LT; / DIV&GT;

\r

\r
\r

我知道,使用 ngInit 是最好的方法来初始化变量,但是用户+50的upvoted其他建议, <一href=\"http://stackoverflow.com/questions/16665319/how-to-set-a-variable-inside-of-a-template#comment58716334_35512817\">I'm疑惑:这是一个记录的功能?我找不到这个任何引用。

如果它不是一个特征,而渲染的错误,人们趁着,我应该认为它是不安全的使用,可能是固定在未来<? / p>

解决方案

这不是一个渲染的错误即可。当多个前pressions用分号分隔角前pression计算到最后的前pression。

  {{a = 5; X = 4; Z =你好}}为{{你好}} {{文本2 ='是';}}为{{}}

这是空字符串不会被渲染。

这行为是不会消失。

I've found this highly upvoted answer (+50) and this comment suggesting that ending an expression with a semicolon and double quotes, i.e. ;"", could be used when you want to define an Angular variable in your template without rendering it as text.

I've tested and it works. Example:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="">
  <p>This variable is rendered: {{ text1 = 'yes' }}   </p>
  <p>This variable isn't:       {{ text2 = 'yes';"" }}</p>  
</div>

I know that using ngInit is the best approach to initialize a variable, but +50 users upvoted the other suggestion and I'm wondering: is this a documented feature? I could not find any reference to this.

If it isn't a feature and rather a rendering bug that people are taking advantage of, should I assume that it isn't safe to use and likely to be fixed in the future?

解决方案

It is not a rendering bug. When multiple expressions are separated by a semicolon the Angular expression evaluates to the last expression.

 {{a=5; x=4; z="hello"}} evaluates to {{"hello"}}

 {{ text2 = 'yes';"" }} evaluates to {{""}}

An empty string is not rendered.

That behavior is not going to go away.

这篇关于尾随AngularJS前pressions双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆