我如何将angularjs attrs转换为数组

时间:2015-07-09 18:54:20

标签: javascript arrays angularjs angularjs-directive attributes

试图找出一种好方法,将传递给角度指令的未知数量的属性转换为数组以用于odata目的。

<my-directive pagenum="3" itemcount="40" name="Thompson" something="blah" ></my-directive>

..我的指令.js文件目前包含

 link: function(scope, elem, attrs) {
    //pseudo code//
    var attrArray = {};
    foreach (attrs.$attr as someattrname){
       attrArray[someattrname] = attrs.someattrname; // Of course this doesnt work.
    }
    scope.runListQuery(attrArray);  // they get serialized into an odata url in here
 },
 controller:['$scope','myDataFactory', function($scope,myDataFactory) {

    $scope.runListQuery = function(attrs) {
      myDataFactory.getQuery(attrs).success(function(data){
        $scope.items = data;
      });
    };

1 个答案:

答案 0 :(得分:1)

这是一个基本上能满足您需求的功能。您可以使用它来获得您想去的地方。

function linkFunc(scope, elem, attrs) {
  for (var property in attrs.$attr) {
    if (attrs.hasOwnProperty(property)) {
      console.log(property);        // The attribute name
      console.log(attrs[property]); // The attribute value
    }
  }
}