Angularjs: Get element in controller

前端 未结 4 1832
暖寄归人
暖寄归人 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:15

    I dont know what do you exactly mean but hope it help you.
    by this directive you can access the DOM element inside controller
    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

    
    

    then, inside controller :

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

提交回复
热议问题