在AngularJS表达式中使用变量?

编程入门 行业动态 更新时间:2024-10-12 20:21:15
本文介绍了在AngularJS表达式中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是Angular的新手,但我想知道我是否可以在AngularJS表达式中使用JS变量(不确定它是否称为表达式).我可以演示的最简单方法是:

I'm new to Angular, but I was wondering am I able to use a JS variable within an AngularJS expression (not sure if it's called an expression). The simplest way I can demonstrate is this:

<code> <script src= "ajax.googleapis/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <script> var number = 10; </script> <div ng-app=""> <p>My expression: {{ 5 + number}}</p> </div> </code>

有办法吗?

推荐答案

否,您不能在Angular表达式中使用全局变量(或函数).角表达式只是属性,字符串而不是Javascript代码也是如此.角度解析器( $ parse 服务,源代码)获取这些属性,对其进行解析并求值针对Angular应用的作用域对象.因此,为了在Angular表达式中使用任何内容,必须在相应的作用域对象上进行定义.

No, you cannot use global variables (or functions) in Angular expressions. Angular expressions are just attributes, so are strings and not Javascript code. Angular parser ($parse service, sourse code) takes those attributes, parses them and evaluates against scope object of the Angular app. So in order to use anything in Angular expression it must be defined on the corresponding scope object.

更多推荐

在AngularJS表达式中使用变量?

本文发布于:2023-11-22 01:30:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   变量   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!