Angularjs: Get element in controller

前端 未结 4 1833
暖寄归人
暖寄归人 2020-12-30 18:23

I am new to angularjs, I know that $scope represent a connection between the controller and the view, But is there a way besides looking for class=\"ng-sc

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-30 19:19

    $element is one of four locals that $compileProvider gives to $controllerProvider which then gets given to $injector. The injector injects locals in your controller function only if asked.

    The four locals are:

    • $scope
    • $element
    • $attrs
    • $transclude

    The official documentation: AngularJS $compile Service API Reference - controller

    The source code from Github angular.js/compile.js:

     function setupControllers($element, attrs, transcludeFn, controllerDirectives, isolateScope, scope) {
        var elementControllers = createMap();
        for (var controllerKey in controllerDirectives) {
          var directive = controllerDirectives[controllerKey];
          var locals = {
            $scope: directive === newIsolateScopeDirective || directive.$$isolateScope ? isolateScope : scope,
            $element: $element,
            $attrs: attrs,
            $transclude: transcludeFn
          };
    
          var controller = directive.controller;
          if (controller == '@') {
            controller = attrs[directive.name];
          }
    
          var controllerInstance = $controller(controller, locals, true, directive.controllerAs);
    

提交回复
热议问题