0

Trying to add li from template and add items from $scope.menuItems (controller) Items don't display http://plnkr.co/edit/Jo7Vml?p=preview

app.directive("menuDemo", function(){
   return {
      templateUrl: 'tree-renderer.html'
   }
});

2 Answers 2

1

Here you go : http://plnkr.co/edit/8YV6cdCIPCjEOq3DtjAz?p=preview

First : added the following to the directive :

 scope:{
          menuItems:"=menuDemo"
  },  

This enable the two-way binding on the data you pass to the menu-demo attribute when using your directives, without this you can't pass data to your directive.

Second : remove the script tag in the tree-renderer.html and sub-tree-renderer.html. Script tag is only usefull when using ng-include along with it. You were just define a template part without using it like this.

Third adding a top level ng-repeat for the 1st level in tree-renderer.html and remove the one from index.html.

<div ng-repeat="menuItem in menuItems">

As you can see menuItems match with what i have defined in the scope:{} part.

Fourth : I added the binding of the function addItem and addSubItem so you can define your own way of adding items to the tree outisde of the directive.

EDIT : Another plnkr where i move out the <div ng-repeat="menuItem in menuItems"> http://plnkr.co/edit/MZ3nsY6WTG3EiNKhXjAv?p=preview.

As you can see i put the menu-demo call in a <span> tag within the <li> tag. This is because they both use isoled scope and if i want to use data from the ng-repeat loop the easiest way is to move the other directive in a child DOM element.

Note that i also change the name of the binding element to menuItem (i remove the 's').

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

7 Comments

Sorry, but first div tag in 'tree-renderer.html' is unnecessary for me. How can I transfer 'ng-repeat' from that div ?
This doens't work: <li ng-repeat="menuItem in menuItems" class="menu" menu-demo="menuItems" menu-add-item="addItem" menu-add-sub-item="addSubItem"></li>
It's better but do you have any other ideas? That span breaks css :(
i would need the said css for this or that you point me what is not working, i don't have anything that seems to break in my fiddle.
i just retested without the span and it work now... strange here is the line : <li ng-repeat="menuItem in menuItems" class="menu" menu-demo="menuItem" menu-add-item="addItem" menu-add-sub-item="addSubItem"></li>
|
0

That is not a correct way to declare a directive try something like this:

angular.module('myModule')
    .directive('menu', [function () {
        return {
            restrict: 'A',
            scope: true,
            templateUrl: 'tree-renderer.html',
            link: function ($scope, $element, $attrs) {
                //here some functions that do something
            }
        };
}]);

By the way, try improve your questions!

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.