How can I get my compiled array from ng-options in a custom directive?

廉价感情. 提交于 2020-01-02 06:38:13

问题


I have a custom directive which I am decorating a <select ng-options=""> with as such...

<select custom ng-model="selected" ng-options="o for o in values">

with custom as my directive and values being a simple array. Here is my implementation...

<select custom ng-model="selected" ng-options="o for o in values">
    <option value selected>uhh?</option>
</select>

app.directive('custom', [function() {
    return {
        scope: {},
        link: function(scope, elem, attrs) {
            // how can I get my array of values here?
        }
    }
}])
app.controller('ctrl', ['$scope', function($scope) {
    $scope.values = ['er', 'um', 'eh'];
}])

Within my link I can see it as such

console.log(attrs.ngOptions);

Which, in this case, logs out the literal "o for o in values". Can I somehow parse or compile this within my link to get the array? I see I can grab it if I do something like scope.$parent.values, but this seems unnecessary and I would need to know the name of "values". I can probably get it through some hacky feeling string manipulation to target it, but I am hoping there is a more intuitive way.

hacky e.g.

var array = attrs.ngOptions.split(' ').pop(); // "values"

console.log(scope.$parent[array]);

Side note - constricted to AngularJS 1.2.x for this example

JSFiddle Link - example


回答1:


As of Angular v1.4, neither select nor ngOptions directives provide an API to get the array of items that results in <option>s, so we are only left with 2 choices - 1) pass the values array explicitly to custom directive as an attribute value, or 2) derive it from the micro-syntax of ng-options.

With #1 - the approach is straightforward.

With #2 - we would need to parse the microsyntax, for example with RegExp. This is fragile, since the micro-syntax may change in the future.

We could use Angular's own regex expression (see src v1.4.3) to parse this syntax:

var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?(?:\s+disable\s+when\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/;

(the 8th group matches the items)

Or, we could make it a simpler regex, perhaps even stabler, for example:

/^.*\s+in\s+(\S+)[\s\S]*$/

At any rate, the directive would look like so:

app.directive('custom', function($parse) {
  return {
    scope: {},
    link: function(scope, elem, attrs) {
      var optionsExp = attrs.ngOptions;

      var match = optionsExp.match(NG_OPTIONS_REGEXP);

      var valuesExp = match[8];

      var valuesFn = $parse(valuesExp);

      var values = valuesFn(scope.$parent);

      // or simpler: 
      // var values = $parse(match[8])(scope.$parent);
    }
  }
})



回答2:


Look the fiddle below. JsFiddle

<div ng-app="app" ng-controller="ctrl">
    <select custom ng-model="selected" ng-options="o for o in values">
        <option value selected>uhh?</option>
    </select>
</div>


var app = angular.module('app', []);

app.directive('custom', [function(){
    return {
        link: function(scope, elem, attrs) {
            console.log(scope.values)
        }
    }
}])
app.controller('ctrl', ['$scope', function($scope){
    $scope.values = ['er', 'um', 'eh'];
}])


来源:https://stackoverflow.com/questions/32014428/how-can-i-get-my-compiled-array-from-ng-options-in-a-custom-directive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!