angularjs - Pass expression to directive -
i have directive, has directive in it's template.
<nv-select ng-model="from" ng-options="item.name item item in from"></nv-select> here try pass along expression ng-options of child directive. unfortunately gives me following error
error: syntax error: token 'as' unexpected token @ column ... if put expression in ng-options of child directive's select, works fine. nv-select directive looks this:
function () { return { restrict: 'e', // restrict elements replace: true, transclude: true, scope: { ngmodel: "=", ngoptions: "&", placeholder: '@' }, template: [ '<div class="nv-select">', '<select ng-model="ngmodel" ng-options="ngoptions" ng-transclude></select>', '<span class="icon suffix-icon-down">{{ text || placeholder }}</span>', '</div>' ].join(''), link: function (scope, elem, attr) { var select = elem.find('select'), copyvalues = function (e) { if (e.options) { scope.text = angular.element(e.options[e.selectedindex]).text(); } }; copyvalues(elem[0]); elem.bind('click', function (event) { elem.toggleclass('active'); }); select.bind('change', function (event) { scope.$apply(function () { copyvalues(event.target); }); }); } }; }; nv-select wrapper around <select> enable custom styling.
do have take special considerations when passing along expression? doing wrong?
let's see how ngoptions implemented in angularjs sources. here can find regexp pattern expressions:
var ng_options_regexp = /^\s*(.*?)(?:\s+as\s+(.*?))?(?:\s+group\s+by\s+(.*))?\s+for\s+(?:([\$\w][\$\w\d]*)|(?:\(\s*([\$\w][\$\w\d]*)\s*,\s*([\$\w][\$\w\d]*)\s*\)))\s+in\s+(.*)$/ and then
if (! (match = optionsexp.match(ng_options_regexp))) { throw error( "expected ngoptions in form of '_select_ (as _label_)? (_key_,)?_value_ in _collection_'" + " got '" + optionsexp + "'."); } var displayfn = $parse(match[2] || match[1]), valuename = match[4] || match[6], keyname = match[5], groupbyfn = $parse(match[3] || ''), valuefn = $parse(match[2] ? match[1] : valuename), valuesfn = $parse(match[7]); so can use or write new.
Comments
Post a Comment