1

I'm trying to create a Directive that contains another directive (defined in the AngularJS UI), and it doesn't seem to work.

Here's my html:

<div class="col-md-12" ng-show="continent == '2'">
       <my-rating></my-rating>
</div>

And the directive:

.directive('myRating', function() {

return{
    restrict: 'E',
    template: '<div class="row question">{{questions.3A.name}}</div> \
              <div class="row rating" ng-controller="RatingDemoCtrl"> \
                <rating value="rate" max="max" readonly="isReadonly" state-on="\'glyphicon-star rated\'" state-off="\'glyphicon-star\'"></rating> \
                  <div class="col-md-12"> \
                    <button class="btn btn-sm btn-danger form-control" ng-click="rate = 0" ng-disabled="isReadonly">Clear</button> \
                  </div>  \
              </div>',
    replace: true,
    scope: {
        text: '@'
    }
};
})

I've copied such directive from a working one, and as soon as I delete code and leave only the div class="col-md-12", then it works. Otherwise, Angular crashes.

What am I missing?

EDIT: I've added a Plunker. The problematic directive is in line 34-42 in script.js), otherwise everything works fine.

EDIT 2: This is the console Error:

Error: [$compile:tplrt] http://errors.angularjs.org/1.2.13/$compile/tplrt?p0=myRating&p1=
at Error (native)
at file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:6:450
at Wa (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:51:416)
at L (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:43:62)
at L (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:43:184)
at L (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:43:184)
at L (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:43:184)
at L (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:43:184)
at Y (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:41:360)
at Wa (file:///C:/Users/Zuh/Desktop/ANGULARJS/js/lib/angular.min.js:51:164) angular.min.js:85
3
  • 1
    can you create a plunkr? Commented Mar 1, 2014 at 16:16
  • 4
    I think your template should have a single root element. Wrap the complete template inside a div. And see the browser console for errors. Commented Mar 1, 2014 at 16:18
  • 2
    What does the error in the console say? Probably is complaining about not having a root element. Commented Mar 1, 2014 at 16:19

2 Answers 2

2

From your plunker:

Error: [$compile:tplrt] Template for directive 'myRating' must have exactly one root element.

Sign up to request clarification or add additional context in comments.

Comments

1

As both Chandermani and Matt pointed out, the issue was the template not having a root element. An extra div fixed it. Thanks guys!!

.directive('myRating', function() {

return{
    restrict: 'E',
    template: '<div>\
                <div class="row question">{{questions.3A.name}}</div> \
                <div class="row rating" ng-controller="RatingDemoCtrl"> \
                  <rating value="rate" max="max" readonly="isReadonly" state-on="\'glyphicon-star rated\'" state-off="\'glyphicon-star\'"></rating> \
                  <div class="col-md-12"> \
                    <button class="btn btn-sm btn-danger form-control" ng-click="rate = 0" ng-disabled="isReadonly">Clear</button> \
                  </div>  \
                </div> \
              </div>',
    replace: true,
    scope: {
        text: '@'
    }
};


})

1 Comment

This Didn't work for me. My template is the same as your solution.

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.