简体   繁体   English

Angularjs:获取控制器中的元素

[英]Angularjs: Get element in controller

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-scope" to get the scope element, I mean something like that: 我是angularjs的新手,我知道$scope表示控制器和视图之间的连接,但除了查找class="ng-scope"以获取scope元素之外,还有其他方法,我的意思是:

function someControllerFunc($scope){
       $scope.element;
}

I know that same controller can be assigned to multiple scopes, So maybe it is not possible. 我知道可以将同一个控制器分配给多个范围,所以也许这是不可能的。

You can pass in the element to the controller, just like the scope: 您可以将元素传递给控制器​​,就像作用域一样:

function someControllerFunc($scope, $element){

}

$element is one of four locals that $compileProvider gives to $controllerProvider which then gets given to $injector . $element是四个当地人一个$compileProvider给予$controllerProvider然后把它交给$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 官方文档: AngularJS $ compile Service API Reference - controller

The source code from Github angular.js/compile.js : 来自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);

I dont know what do you exactly mean but hope it help you. 我不知道你究竟是什么意思,但希望它对你有帮助。
by this directive you can access the DOM element inside controller 通过此指令,您可以访问控制器内的DOM元素
this is sample that help you to focus element inside controller 这个示例可以帮助您将元素集中在控制器中

.directive('scopeElement', function () {
    return {
        restrict:"A", // E-Element A-Attribute C-Class M-Comments
        replace: false,
        link: function($scope, elem, attrs) {
            $scope[attrs.scopeElement] = elem[0];
        }
    };
})

now, inside HTML 现在,在HTML中

<input scope-element="txtMessage" >

then, inside controller : 然后,内部控制器:

.controller('messageController', ['$scope', function ($scope) {
    $scope.txtMessage.focus();
}])

Create custom directive 创建自定义指令

masterApp.directive('ngRenderCallback', function() {
    return {
        restrict: "A",
        link: function ($scope, element, attrs) {
            setTimeout(function(){ 
                $scope[attrs.ngEl] = element[0];
                $scope.$eval(attrs.ngRenderCallback);               
            }, 30);
        }
    }
});

code for html template html模板的代码

<div ng-render-callback="fnRenderCarousel('carouselA')" ng-el="carouselA"></div>

function in controller 控制器中的功能

$scope.fnRenderCarousel = function(elName){
    $($scope[elName]).carousel();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM