AngularJS指令用于获取元素ID

7

我需要从AngularJs中获取元素ID。
我尝试过这个方法,但它没有起作用。

angular.module('AngStarter').directive('oblInLineEditor', function() {
    return function(scope, element, attr) {
         console.log("value = " + scope.$eval(attr.id));
    }
});
2个回答

14

这里不需要使用 scope.eval。

angular.module('AngStarter').directive('oblInLineEditor', function() {
    return function(scope, element, attr) {
         console.log("value = " + attr.id);
    }
});

5
请看这个例子:http://jsfiddle.net/kevalbhatt18/bu6jxzan/
var myApp = angular.module('AngStarter', []);

myApp.directive("oblInLineEditor", function(){
    return {
        restrict: "E",
        link: function(scope, elem, attrs) {
            console.log(elem[0].id);
            console.log(attrs.id)

        }
    }
});


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接