1

I have an existing HTML based web UI for an administration panel.

I am trying to take all of the HTML components, and turn them into re-usable AngularJS directives so I can embed them in a page with minimal effort.

Here is one I'm stuck on:

<section class="dash-tile">
    <div class="tile-title" ng-controller="testCtrl">{{test}}</div>
    <div class="tile-stats"><b>8068</b>
    </div>
    <div class="progress progress-xs mt5 mb10">
        <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">
        </div>
    </div>
    <div class="tile-footer">
        Based on new sales
    </div>
</section> 

This snippet, used inside of my .html GUI will create a small box shaped title with some data inside.

Could someone give me an example of how to turn this into a reusable component?

So far I'm here:

admin.directive('whiteBox', function () {
  return {
    restrict: 'EA', //restrict the directive to ONLY an attribute or element
        replace: true,
    transclude: true,
    template: '<section class="dash-tile">' +
                                    '<div class="tile-title" ng-controller="testCtrl">' + '{{test}}' + '</div>'
                                    + '<div class="tile-stats">' + '<b>' + '8068' + '</b>'
                                    + '</div>'
                                    + '<div class="progress progress-xs mt5 mb10">'
                                        + '<div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width: 40%">'
                                    + '</div>'
                                    + '</div>'
                                    + '<div class="tile-footer">'
                                    +   'Based on new sales'
                                    + '</div>'
                                + '</section> ',
        link: function (scope, element, attrs) {
                // add events
        }
    };

But this breaks the application for some reason.

7
  • 1
    Define "breaks the application for some reason". Commented Dec 15, 2014 at 18:58
  • No console errors, everything is fine in the console. But the directive does not load on screen. Commented Dec 15, 2014 at 19:02
  • Well, if that's your full directive, you're missing a closing curly brace, parenthesis, and semicolon. It seems to work fine other than that see jsBin Commented Dec 15, 2014 at 19:09
  • I don't see you using the directive in html ... Commented Dec 15, 2014 at 19:09
  • It is embedded using <div whiteBox></div> in the html Commented Dec 15, 2014 at 19:10

1 Answer 1

1

AngularJS uses the following convention

If you name your directive whiteBox, in HTML you use it white-box.

AngularJS declaration

app.directive('myDirective', function () {

HTML usage

<div my-directive></div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.