1

I want to pass array in the directive attribute. It doesn't work. I tried to pass an array in the attribute of a directive and nothing happened.

class Directive {
  constructor () {
    'ngInject';

    let directive = {
      restrict: 'E',
      template: '<div class="btn-group">
                   <label class="btn btn-primary classButton" 
                          ng-model="radioModel" uib-btn-radio="'First'" 
                          config="{{config[0]}}">{{config[0]}}
                   </label>
                 </div>',

      controller: Controller,
      controllerAs: 'vm',
      bindToController: true,
      scope: {
        config: '='
      }
    };

    return directive;
  }
}

class Controller {
  constructor() {
    'ngInject';
  }
}
<directive config="First, Second"></directive>

How should I pass the array to the directive?

3
  • 1
    try [First, Second]. You don't pass in an array, but invalid notations. Put [] around them to convert it to an array Commented Nov 10, 2015 at 10:51
  • 1
    You should store array in the $scope or vm and pass it as by reference because you're using two-way binding. If you just pass it in the attribute it might cause issues. Commented Nov 10, 2015 at 10:53
  • remove the {{ and }} Commented Nov 10, 2015 at 11:29

1 Answer 1

1

Maksim,

You need to pass a scope variable instead of a array constant here,

That is, declare a scope variable and assign this value to that variable like this,

$scope.myArray = ['Firs', 'Second'];

and now you can pass this scope variable to your directive.

<directive config="myArray"></directive>

It should work !!!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.