javascript - Double curly braces {{}} problem in angular.js
ringa_lee
ringa_lee 2017-07-04 13:44:52
0
4
1992
<p ng-controller="listCtrl">
    <h1>这是列表页面</h1>
    <ul>
        <li ng-repeat="item in myList"><a href="#/myDetail/{{$index}}">{{item}}</a></li>
        <li ng-repeat="item in myList"><button ng-click="jump('/myDetail/'+$index)">{{item}}</button></li>
    </ul>

</p>

As the title says, in the above code, I need to get the $index value as a parameter. I need to add {{}} to the first li to get the value, but there is no need to add it to the second li; how should I do this? distinguish? ?

ringa_lee
ringa_lee

ringa_lee

reply all(4)
伊谢尔伦

In order to parse data in native HTML, you need to use {{}} to bind data, but there is no need for this behind the ng command.

伊谢尔伦

{{ $var }} wrapped template variable , used when directly output the variable value without additional parsing (such as filter processing, as an instruction parameter, as a function parameter), otherwise, ng -click="jump('/myDetail/'+ $index)" is the parameter called in the command.

ringa_lee

The simple criterion is

  • If the type assigned to the current attribute is a literal type, such as an attribute in an html template, or an attribute with @ as a modifier in a directive, then if you do not add double curly brackets when assigning the value, it will be a literal value. To calculate, if you want to parse it as a variable, you need to add double curly braces.

  • If the type assigned to the current attribute is a non-string type, such as various built-in instructions in the angular template, or attributes modified with <, =, & in the instructions, then the assignment will be as follows The syntax rules of js are parsed. In this case, there is no need to add curly brackets.

滿天的星座

First explain the two types in Angular:

Template: The format is {{...}}, which is essentially a string, and will be expanded into a recognizable string text by the parsing engine
Expression: code that can be run directly, basically the same as JS writing, except All this omitted.

Then you can refer to the context to determine whether you are writing a JS-like part or writing a string literal, so that it is easy to judge whether you need a template or an expression.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template