search

Home  >  Q&A  >  body text

javascript - Double curly braces {{}} problem in angular.js

<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_leeringa_lee2694 days ago1975

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-07-04 13:46:52

    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.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-07-04 13:46:52

    {{ $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.

    reply
    0
  • ringa_lee

    ringa_lee2017-07-04 13:46:52

    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.

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-04 13:46:52

    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.

    reply
    0
  • Cancelreply