0

Trying to figure out a good way to convert an unknown quantity of attributes passed into an angular directive into an array for odata purposes.

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

..My directive .js file contains so far

 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;
      });
    };
2
  • That would probably work if you declare attrArray as an array instead of an object. Try var attrArray = []; Commented Jul 9, 2015 at 19:03
  • @Blackhole Makes sense actually, that sub object gives you just the attributes that were put on the element. Commented Jul 9, 2015 at 19:12

1 Answer 1

1

Here's a function that does basically what you want. You can use this to get where you are trying to go.

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
    }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Realized I can't use angular.forEach here because the keys and values are coming from two different places.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.