Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS multiple directives in HTML. Only one shows up

Tags:

angularjs

Working with this repo: https://github.com/nkcraddock/angular-playing-cards

In that demo, the following code works and you see a list of all of the cards.

<div ng-controller="DemoCtrl" style="font-size: 0.45em;">
    <playing-card suit="{{card.suit}}" rank="{{card.rank}}" ng-repeat="card in Cards"/>
</div>

In my page, the following code does not work. Only the first card shows up. The ace.

<div>
  <playing-card rank="ace" suit="spade" />
  <playing-card rank="king" suit="spade" />
</div>

But the following code DOES work. Both cards show up. Why is this?

<div>
  <playing-card rank="ace" suit="spade" />
</div>
<div>
  <playing-card rank="king" suit="spade" />
</div>
<div>

For the full code, check the repo. But the directive is below in case it helps.

return {
    scope: {
        rank: '=',
        suit: '='
    },
    restrict: 'E',
    // template: function(tElement, tAttrs) {
    //     return ranks[tAttrs.rank].template;
    // },
    link: function(scope, element, attrs) {
        scope.rank = ranks[attrs.rank] || ranks.back;
        scope.suit = suits[attrs.suit] || suits.heart;
        element.replaceWith($compile(scope.rank.template)(scope));
    }
};
like image 772
Eric Ihli Avatar asked Nov 28 '15 04:11

Eric Ihli


People also ask

Can we use multiple directives in AngularJS?

Combining structural directives can lead to unexpected results however, so Angular requires that an element can only be bound to one directive at a time. To apply multiple directives we'll have to either (a) expand the sugared syntax, (b) nest template tags or use the <ng-content> tag.

Can an HTML page have multiple NG-App directive for bootstrapping?

The ng-app directive tells AngularJS that this is the root element of the AngularJS application. All AngularJS applications must have a root element. You can only have one ng-app directive in your HTML document.

What does $compile do in AngularJS?

Overview. Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together. The compilation is a process of walking the DOM tree and matching DOM elements to directives. Note: This document is an in-depth reference of all directive options.

What are the three important directives of AngularJS?

Following are the most common directives: ng-app: This directive starts an AngularJS Application. ng-init: This directive initializes application data. ng-model: This directive defines the model that is variable to be used in AngularJS.


1 Answers

Figured it out... You have to close the directive element.

<div>
  <playing-card rank="ace" suit="spade"></playing-card>
  <playing-card rank="king" suit="spade"></playing-card>
</div>

That works.

<div>
  <playing-card rank="ace" suit="spade" />
  <playing-card rank="king" suit="spade" />
</div>

That doesn't work.

like image 199
Eric Ihli Avatar answered Oct 05 '22 08:10

Eric Ihli