0

OK, so I have a directive which takes attributes and reads it (and writes it out).

Here is the plunker: http://embed.plnkr.co/IkKPLahPc9yqeHWEQUG3/

I think it's because of the controller: ctrl inside main-directive.js which has nothing whereas the actual action is happening inside the isolated directive's controller controller.

Here is the main-directive.js:

var app = angular.module('testapp.directive.main', ['main']);


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

  var controller = ['$scope', function($scope) {

    $scope.dan = { 'name': 'Dan', 'nationality': 'ESP' };
    // scope from here obv...

  }];

  var template = 'Getting attribute value of =getInfo... {{getInfo.name}} from {{getInfo.nationality}}';

  return {
    restrict: 'E',
    controller: controller,
    scope: {
      getInfo: "=info"
    },
    template: template
  };
});

app.controller('ctrl', function($scope) {

})

and here's my template:

<div ng-controller="ctrl">
    <my-customer info="dan">
    </my-customer>
</div>

Why is my directive not reading the attribute of info?

2 Answers 2

1

You're right, the $scope.dan object needs to be in the ‘ctrl’ controller scope and pulled out of the isolate directives controller scope.

app.controller('ctrl', function($scope) {
    $scope.dan = { 'name': 'Dan', 'nationality': 'ESP' };
})

This is applicable to the method of two-way data binding that you have set up for getInfo used by "=info"

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

7 Comments

This is the correct answer. I didn't even catch that it was on the directive's controller scope
I usually write controller methods directly within the return {} object for clarity.
But if you don't like that, you can also pull out the function expression and point to a function Declaration(){}, for example, use { controller: Declaration } within the return body.
I typically do app.controller("MyDirectiveController") as a totally separate thing and then reference it in {controller: 'MyDirectiveController'}. I like having the controller completely separate for easier unit testing.
I see where my problem lies. But I'd like to have the data in the isolated controller controller inside the directive instead of the controller ctrl. How do I do that?
|
0

The way that is coded, it is expecting the ctrl controller to have a property called "dan" on its scope. If you are just passing in the string 'dan', you want to change your directive to use @ instead of =

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

        var controller = ['$scope', function ($scope) {

            $scope.dan = {'name': 'Dan', 'nationality': 'ESP'};
            // scope from here obv...

        }];

        var template = 'Getting attribute value of =getInfo... {{getInfo.name}} from {{getInfo.nationality}}';

        return {
            restrict: 'E',
            controller: controller,
            scope: {
                getInfo: "@info" //<--NOTE THE CHANGE HERE
            },
            template: template
        };
    });     

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.