I'm trying to encapsulate a select in a custom directive while keeping the usage of ng-options on the directive declaration.
The goal is to be able to keep the syntax of the ng-options in the template using my directive, but to correctly forward it to the ng-options of the select inside the directive.
Well more that words, code :
The directive :
angular.module('myApp').directive("selectField", function() {
return {
restrict: 'EA',
replace: true,
transclude : false,
templateUrl : "/common/tpl/form/select-field.html",
scope : {
label : "@",
model : "=",
options : "&"
}
};
});
The template :
<div class="form-group">
<label for="{{fieldId}}" class="col-lg-2 control-label">{{label | translate}}</label>
<div class="col-lg-10">
<select class="form-control" id="{{fieldId}}" ng-model="model" ng-options="{{options}}"></select>
</div>
</div>
The usage :
<select-field label="myLabel" model="data.mySelectValue" options="p.nom for p in myOptions"></select-field>
And ... the error :
Error: [$parse:syntax] Syntax Error: Token 'for' is an unexpected token at column 7 of the expression [p.nom for p in preteurs] starting at [for p in preteurs].
I tryed with options as "&" , "=" and "@" attributes, but nothing seems to be working.
With "=" and "&", angular explode with the given error, and with "@" attributes, the expression is evaluated in the directive scope, which work but render no options because "myOptions" doesnt exists in the directive's own scope...
Am I missing something ? It there a way to do this ?