Nick Heiner
Nick Heiner

Reputation: 122560

Syntax error with uiSelect2

I'm trying to use string concatenation in a ng-repeat in a ui-select2:

    <select ui-select2 ng-model="selected2" style="width:300px;">
        <option ng-repeat="item in data[it + 'ems']" selected 
                value="{{item}}">{{item}}</option>
    </select>

(This is a trivial example, but the string concatenation is necessary for code reuse.)

It works just fine on a normal select. However, uiSelect2 produces an error:

Error: Syntax Error: Token ']' is an unexpected token at column 6 of the expression ['ems']] starting at []].
    at Error (<anonymous>)
    at throwError (http://code.angularjs.org/angular-1.0.1.js:5830:11)
    at parser (http://code.angularjs.org/angular-1.0.1.js:5824:5)
    at http://code.angularjs.org/angular-1.0.1.js:6387:29
    at compileToFn (http://code.angularjs.org/angular-1.0.1.js:8040:16)
    at Object.Scope.$watch (http://code.angularjs.org/angular-1.0.1.js:7547:19)
    at http://angular-ui.github.com/angular-ui/build/angular-ui.js:911:19
    at nodeLinkFn (http://code.angularjs.org/angular-1.0.1.js:4223:13)
    at compositeLinkFn (http://code.angularjs.org/angular-1.0.1.js:3838:14)
    at compositeLinkFn (http://code.angularjs.org/angular-1.0.1.js:3841:12) <select ui-select2="" ng-model="selected2" style="width:300px;" class="ng-pristine ng-valid"> 

Reproduced in this fiddle.

Am I doing something wrong here?

Upvotes: 0

Views: 480

Answers (1)

Ben Lesh
Ben Lesh

Reputation: 108491

I suspect it's because select-ui has an isolated scope, and so does ng-repeat, which puts your it variable out of reach of that ng-repeat.

I recommend using ng-options for selects anyhow.. (which works, presumably because it prevents the scoping issue I mentioned):

<select ui-select2 ng-model="selected2" style="width:300px;" 
  ng-options="item for item in data[it + 'ems']"></select>

Upvotes: 1

Related Questions