javascript - AngularJS: What is this directive doing? -
javascript - AngularJS: What is this directive doing? -
i have experience angularjs , have come across directive on web, not have seen before , unable comprehend doing? can help?
specific questions: little understanding, signature of directive must doing dependency injection. struggling is: if $injector passed in array, why sent parameter in function i.e. function($injector); in other words why there 2 $injectors? not work if dont send $injector in array?
also how directive has got controller embedded? when define such controllers?
also see scope $ prefix in the code below how working without $?
any links read more or explaining here useful.
.directive('mycomp', [ '$injector', function($injector) { var $builder, $compile, $drag; $builder = $injector.get('$builder'); $drag = $injector.get('$drag'); $compile = $injector.get('$compile'); homecoming { restrict: 'a', scope: { component: '=mycomp' }, controller: 'mycompcontroller', link: function(scope, element) { scope.copyobjecttoscope(scope.component); $drag.draggable($(element), { mode: 'mirror', defer: false, object: { componentname: scope.component.name } }); homecoming scope.$watch('component.template', function(template) { var view; if (!template) { return; } view = $compile(template)(scope); homecoming $(element).html(view); }); } }; } ])
why sent parameter in function i.e. function($injector); in other words why there 2 $injectors?
when doing array-type injection doesn't matter how parameter in function called, map array items. example, if have
['$injector', function(a) {..}]
parameter a
map $injector
instance, , if have
['$injector', '$scope', function(a, b) {..}]
a
map $injector
instance , b
map $scope
instance. order here matters. more here: https://docs.angularjs.org/tutorial/step_05 in note on minification section.
what not work if dont send $injector in array?
if don't, $injector undefined, of explained above.
also how directive has got controller embedded? when define such controllers?
some directives can have controllers if needed, should hold heavier logic, $scope binding , on. link function should hold interactions $element. more here: http://www.sitepoint.com/practical-guide-angularjs-directives/
also see scope $ prefix in the code below how working without $?
in case scope
used in link function , not injectible. in case simple variable scope
refers controllers scope. can phone call superbigvariablename
, still refer scope , still work. maintain in mind, link function simple function first attribute scope, sec element, 3rd attributes, can't inject services link function (do logic in controller)
javascript angularjs angularjs-directive angularjs-scope angularjs-service
Comments
Post a Comment