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));
}
};
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With