0

I'm new to and have a bit of trouble trying to do this simple task of updating a scope variable I set in my directive in the calling controller.

So I have a directive:

app.directive('pageShell', function (){
    return{
        restrict: 'AE',
        transclude: true,
        replace: true
        scope:{
            title: '@',
            body: '@',
            footer: '@'
        },
        template: '<div class="title-content">{{title}}</div>' +
                  '<div class="body-content">{{body}}</div>' +
                  '<div class="footer-content">{{footer}}</div>'
    }
});

so in my html:

<page-shell title="Header" body="this is my body" footer="Footer" />

in my controller:

app.c

ontroller("myCtrl", function ($scope) {

    // TEST 1 - simple scope change
    // switch pages or content so change some values
    if(pageIndex === 2) {
      $scope.title = "Page 2 title";
      $scope.body = "Page 2 body content";
      $scope.footer = "Page 2 footer";
    }

    // TEST 2 - change by using apply
    $scope.$apply(function() {
            $scope.title = "Page 2 title";
            $scope.body = "Page 2 body content";
            $scope.footer = "Page 2 footer";
    });
});

So I tried in my controller thinking I should have access to my variable in my scope to change it directly but it did not work but did not throw any errors. Tried using $apply but got the error $apply is already in progress? Tried changing to 2-way binding in the scope by using = instead of @ for the title, body, and footer but get a $parse:syntax error.

Any clue why this simple task wouldn't work? Thanks!

2
  • If you get an "Apply already in progress" this means that you are running the apply in a place where angular is already applying a change. Possibly this means something else is going on here! Could you post a Fiddle with your code demonstrating the issue? Commented Jan 3, 2014 at 13:57
  • Please fix your code formatting so that it is easy to read. Also please provide the declaration of directive in html. Commented Jan 3, 2014 at 13:59

2 Answers 2

2

Figured it out: My pageShell directive's scope needs to be:

scope:{
    title: '=',
    body: '=',
    footer: '='
},

The binding needs to be both ways.

the html:

<page-shell title="Header" body="this is my body" footer="Footer" />

and the controller:

Controller("myCtrl", function ($scope) {

    // TEST 1 - simple scope change
    // switch pages or content so change some values
    if(pageIndex === 2) {
      $scope.title = "Page 2 title";
      $scope.body = "Page 2 body content";
      $scope.footer = "Page 2 footer";
   }

   // TEST 2 - change by using apply
   $scope.$apply(function() {
   $scope.title = "Page 2 title";
   $scope.body = "Page 2 body content";
   $scope.footer = "Page 2 footer";
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Your HTML should be something like this

<page-shell title="{{title}}" body="{{body}}" footer="{{footer}}" />

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.