2

I'm trying to pass an array from the parent controller into a child component.

<plant-select plants="{{head.permittedPlants}}"></plant-select>

the head.permittedPlants is an array

I get the following error:

"Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 2 of the expression [{{head.permittedPlants}}] starting at [{head.permittedPlants}}]."

can I only pass a string in?

my component looks like this:

let plantSelectComponent = {
  bindings:{
  plants:'<'
},
  template:require('./plantSelect.template.html'),
  controller:plantSelectController
}

controller of the parent below. The "permittedPlantsResolve" is a resolves from UI-Router:

function headerCtrl($scope, $ngRedux, $mdSidenav, $log, permittedPlantsResolve){

  let ctrl = this;


 ctrl.permittedPlants = permittedPlantsResolve;


 ctrl.consoleState = function(){
    console.log($ngRedux.getState());
  }

  ctrl.menuToggle = function(){
    $mdSidenav('left').toggle()
  }

}

export default headerCtrl;

if I change the binding to an "@" it passes the string no problem. Are arrays a possibility? do I HAVE to pass a string?

SOLVED: in the controller for the component I was assigning the $attrs.plants on postLink, assigning it onInit fixed the issue!

1
  • He template file that holds the <plant-select> is not a component, just html with a controller attached. Does it need to be a component as well? Commented Nov 30, 2016 at 18:07

2 Answers 2

3

If you remove the {{}} in your template you should be fine still using one way binding (<). The reasons @ works is because {{}} is evaluating your head.permittedPlants to a string before passing. If you plan on changing your array in your component, then you need to use the two way binding (=).

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

8 Comments

When I do that what gets passed to my component is just a string "head.permittedPlants"
Can you post your controller as well?
Are you sure head is the name of your controller in your template?
Yes. Everywhere else in the template I'm using "head" as the controllerAs and it's working fine. <a ng-click="head.menuToggle()"> etc..
Strange... the only other thing I can think of is your permittedPlantsResolve is not an array...
|
0

Change the bindings of plants from < to = and remove the curly braces around the property in the HTML.

3 Comments

When I do that what gets passed to my component is just a string "head.permittedPlants"
actually, remove the quotes and leave the curly brace
@Ladmerc with braces and no quotes, still same issue. Passes string "head.permittedPlants"

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.