2015-07-06 97 views
6

我需要從AngularJs獲取元素ID。
我試過這個,但沒有奏效。AngularJS指令獲取元素ID

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

回答

13

你不需要scope.eval這裏。

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

看看這個例子: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) 

     } 
    } 
});