1

Let's say for example I have this controller:

    function MainController() {

         var self = this;

         self.employee = {
             name: 'Travis Libby',
             occupation: 'Frontend Engineer'
         };

    }

And I have defined this directive:

    angular
        .module('myApp')
        .directive('employeeCard', function() {
            return {
                restrict: 'AEC',
                template: '<strong>{{ main.employee.name }}</strong>'
            }
    }); 

And my html:

    <body ng-controller="MainController as main">

        <employee-card></employee-card>

    </body>

By using controller as syntax, I now have to reference the alias of my controller inside the directive itself. Doesn't that pattern destroy the point of custom directives? How would I then use this custom directive inside any other controller?

1 Answer 1

5

One way is use isolated scope and pass in employee

<employee-card employee="main.employee"></employee-card>

JS

angular
    .module('myApp')
    .directive('employeeCard', function() {
        return {
            restrict: 'AEC',
            scope: {employee :'='},
            template: '<strong>{{ employee.name }}</strong>'
        }
}); 
Sign up to request clarification or add additional context in comments.

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.