简体   繁体   English

指令属性angularJS中的大括号

[英]Curly braces in directive attribute, angularJS

I have a directive which uses the id parameter to get an array in the controller's scope, which works fine if the id is static value, however if I use curly braces (for example I repeat the directive using ng-repeat and so each id needs to be unique, though if I use another attribute than id it still doesn't work), then it doesn't work, the curly braces are not evaluated, and looking at the source in the web inspector, it is literally id={{index}} rather than id=1 . 我有一个使用id参数来获取控制器范围内的数组的指令,如果id是静态值,它可以正常工作,但是如果我使用花括号(例如我使用ng-repeat重复指令,所以每个id需要要是唯一的,但如果我使用另一个属性而不是它仍然无效),那么它不起作用,花括号不被评估,并且在Web检查器中查看源,它实际上是id={{index}}而不是id=1

EDIT: What I'm trying to do is when there is a stack element in the html, it creates a variable in the current scope (not the directive's scope) with the same name as the id. 编辑:我想要做的是当html中有一个堆栈元素时,它在当前作用域(而不是指令的作用域)中创建一个与id同名的变量。

app.directive('stack', function() {
    return {
        restrict: 'E',
        templateUrl: 'stack.html',
        scope: {
            cards: '=id',
            name: '@id',
            countOnly: '@'
        },
        link: function(scope) {
            scope.cards = [];
            //because of cards: '=id', scope.cards is bound to the parentscope.<value of id attr>, so setting scope.cards to [], also creates the variable in the parent scope.
        }
    };
});

The stack.html template simply contains (at the moment it will be expanded later): stack.html模板只包含(此时它将在稍后展开):

<div class="stack">The stack called {{name}} contains {{cards.length}} cards</div>

So if I have this in my index.html: 所以,如果我在index.html中有这个:

<stack id="deck"></stack>
<span>There are {{deck.length}} cards in deck</span>

The result is: 结果是:

The stack called deck contains 0 cards
There are 0 cards in deck

(and if I were to add a card to the deck array, then both zeros would change to one) (如果我要在牌组中添加一张牌,那么两个零都会变为一个)


The above works, as it doesn't use ng-repeat, the attributes are hardcoded. 以上工作,因为它不使用ng-repeat,属性是硬编码的。 Below the attributes need to be dynamic, and this doesn't work as the curly braces are not evaluated. 属性下面需要是动态的,这不起作用,因为不评估花括号。

However if I want to have multiple decks by using ng-repeat it doesn't work: 但是,如果我想通过使用ng-repeat来创建多个套牌,则它不起作用:

<stack ng-repeat="index in range(5)" id="slot{{index}}"></stack>

then the id remains literally "slot{{index}}", not "slot0", etc. 然后id仍然字面意思是“slot {{index}}”,而不是“slot0”等。

I could do: 我可以:

<stack id="slot0"></stack>
<stack id="slot1"></stack>
<stack id="slot2"></stack>
<stack id="slot3"></stack>
<stack id="slot4"></stack>

and it would work as I expect. 它会像我期望的那样工作。

It's almost like I need the cards: '=id', to be cards: '=$parse(id)' , (or maybe only parse if it contains braces) 这几乎就像我需要cards: '=id', cards: '=$parse(id)' ,(或者只有在包含大括号时才解析)

please see here http://jsfiddle.net/4su41a8e/2/ 请看这里http://jsfiddle.net/4su41a8e/2/

  • curly braces for one way binding 用于单向绑定的花括号
  • no braces for two way binding 没有大括号的双向绑定

HTML: HTML:

<div ng-app="app">
    <div ng-controller="firstCtrl">
        <stack ng-repeat="card in range" item="card" name="{{card.name}}"></stack>
    </div>
</div>

DIRECTIVE: 指示:

app.directive('stack', function () {
    return {
        restrict: 'AE',
        template: '<h2>cards id: {{cards.id}} </h2><p>name:{{name}}</p>',
        scope: {
            cards: '=item',
            name: '@name',
            countOnly: '@'
        }
    };
});

Inside the ng-repeat use: 在ng-repeat中使用:

<stack id="index"></stack>

You don't want the interpolated value, but the reference to the value. 您不需要插值,而是需要对值的引用。

In the directive, you then use data-binding (on that note you can use = or @) to bind the index to the cards variable. 在指令中,然后使用数据绑定(在该注释上,您可以使用=或@)将索引绑定到cards变量。 You now have the actual index value in the link function, accessible through cards. 您现在可以通过卡访问链接功能中的实际索引值。

Here a snippet that contains your exact problem, http://jsbin.com/iMezAFa/2/edit . 这是一个包含您确切问题的片段, http://jsbin.com/iMezAFa/2/edit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM