Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
839 views
in Technique[技术] by (71.8m points)

angularjs - Behavior of controller inside directives

I know that a $scope from a controller can be shared to a link function in directives.

For example, in this code I can call a function from declared controller to print 'Hello World' on browser console:

 .directive('myDirective', [function () {
        return {
            restrict : 'E',
            replace : true,
            controller: 'MyController',
            templateUrl : 'directives/myDirective.tpl.html',
            link : function (scope, elem, attrs, controller) {
                scope.message = 'Hello World!';
            }
        };
    }])

    .controller('MyController', [function ($scope, $element, $attrs, $log, $timeout) {

        // $timeout to wait the link function to be ready.
        $timeout(function () {
            // This prints Hello World as expected.
            $log.debug($scope.message);
         });


        });
    }])

Ok, this works fine.

My questions are:

  1. In this approach, it is the SAME scope that will shared between the controller and the directive?
  2. What is the consequences to use this approach? Let us assume that I will not manipulate DOM elements in controller, only in link function.
  3. I really need to avoid manipulate DOM elements in this controller? Even if the $scope, $elem, etc are the same?

These are questions that I didn't find on Angular Directive documentation.

Here's a plunker with the sample code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In this approach, it is the SAME scope that will shared between the controller and the directive?

Yes, it is.

What is the consequences to use this approach? Let us assume that I will not manipulate DOM elements in controller, only in link function.

The controller is what provides the directive's behavior, just like with a regular Angular application. That said, you should manipulate the scope inside the controller function only. If you need to change the scope from the link function, call a method of it. Besides, since the controller is executed before the link function, you should initialized the scope in the former so the latter can get a valid model to work on.

I really need to avoid manipulate DOM elements in this controller? Even if the $scope, $elem, etc are the same?

By definition, the link function is the place to perform DOM manipulation. I can't find a technical reason that would prevent you from manipulating the DOM inside the directive's controller except that you shouldn't. In fact, in order to check that I've just changed one directive I've written and moved all the code from the link function to the controller function and everything's kept working. But if you mix both scope logic and DOM manipulation together I think it'll be hard to track down what's going on.

Finally, you may find this article useful: Understanding Directives.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...