0

I have a list of person objects. I have a directive for displaying some read only data about the person and nested inside it a directive that acts as the toolbar for actions on the person (delete, friend etc). When you click on the first directive a second nested directive shows up to for editing the person.

In the simple example in plnker this works fine, but in actual life this gets really flimsy with fields not updating, or updating infinitely (editor has quite a few ngRepeats making things even weirder) etc.

It seems awkward that I have 3 isolated scopes and pass the the same object to all three, but on the other hand I need a lot of properties/methods of that object in all 3 directives so it makes sense to pass the whole object. Is there a better way of doing this?

app.directive('personCard', [function () {
    var directive = {
        link: link,
        restrict: 'A',
        templateUrl: 'personcard.tpl.html',
        scope: {
            person: '='
        }
    };
    return directive;

    function link(scope, element, attrs) {
        scope.isOpen = false;

        scope.person.close = function(){
            scope.isOpen = false;  
        }

        scope.person.edit = function (){
            scope.isOpen = true;  
        }
    }
}]);

app.directive('personToolbar', [
  function () {
    var directive = {
        link: link,
        restrict: 'A',
        templateUrl: 'personcardtoolbar.tpl.html',
        scope: {
            person: '=',
            close: '&',
            edit: '&'
        }
    };
    return directive;

    function link(scope, element, attrs) {

    }
}]);

app.directive('personEditor', [
  function () {
    var directive = {
        link: link,
        restrict: 'A',
        templateUrl: 'personeditor.tpl.html',
        scope: {
            person: '='
        }
    };
    return directive;

    function link(scope, element, attrs) {

    }
}]);
2
  • If your directives are always nested, then you don't need the isolate scope on all. The sub directives will inherit them. Also it would be good if you could provide a failing case, since it's not clear what kind of error you expect help with Commented Mar 7, 2014 at 5:59
  • It's not so much of a specific error, rather I am wondering if this is the correct way of going about it. I thought of inheriting the scope but the innermost directive (person Editor) is used on its own as well so I guess this one needs an isolated scope. The toolbar could inherit it. Is there a significant different if the 3 directives use isolated scope vs the same inherited scope for the particular object? Commented Mar 7, 2014 at 13:08

0

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.