从传递的范围/元素中获取元素ID属性?

时间:2014-09-17 01:36:50

标签: angularjs scope pass-by-reference

我试图在更改时从输入字段中获取ID属性。

myApp.controller('OnChangeController', ['$scope', function($scope) {
    $scope.Change = function(e) {
        //alert(e);
        $scope.output = e.$attr;
    }
}]);

    <div ng-controller="OnChangeController">
        <input ng-model="change" id="ChangeID1" ng-change="Change(this)"> {{ output }}
        <input ng-model="change" id="ChangeID2" ng-change="Change(this)"> {{ output }}
    </div>

如何获取元素的ID属性?

1 个答案:

答案 0 :(得分:0)

我想要我的身份证,请注意:P

这是一个很好的,毫无意义的指令来获得它。

我正在编写基于此示例的代码:http://jsfiddle.net/simpulton/E7xER/

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
        <script>
            var firstApp = angular.module('firstApp', []);
            firstApp.directive('myFirstApp', function() {
                var variable = function(scope, element, attrs) {
                    var elemArray = element.children();
                    $.each(elemArray, function() {
                        $(this).on('click', function() {
                            var id = $(this).attr("id");
                            alert(id);
                        });
                    });
                };
                return {
                    restrict: 'AEC',
                    link: variable
                };
            });
        </script>
    </head>
    <body ng-app="firstApp">
        <my-first-app>
            <button id="a1">button 1</button>
            <button id="a2">button 2</button>
        </my-first-app>
        <br />
        <my-first-app>
            <button id="b1">button 1</button>
            <button id="b2">button 2</button>
        </my-first-app>
    </body>
</html>