简体   繁体   中英

how to pass value to directive as a parameter of ng-init

i was trying to pass the list of value when page is loaded. but my ng-init function not even getting trigger. whats the problem with my code.

<li class="tag" ng-repeat="list in displayItems  track by $index"  ng-class="{selected: $index==selectedIndex}"  data-ng-init="display(selecteditemslist,searchid)">
                <span class="tag-label">{{list}}</span><span class="tag-cross pointer" ng-click="Delete($index,selecteditemslist[$index],list,searchid)">x</span>
            </li>

to the function in controller

$scope.display=function(list,searchid){
                console.clear();
                console.info(list);
                console.info(searchid);

                switch(searchid) {

                    case 'organisation' :
for(var i=0; i<list.length; i++){
                        getOrg(list[i]).fetch({}).$promise.then(
                            function (value) {
                                $scope.displayItem = value.data[0];
                                console.clear();
                                console.info($scope.displayItem);
                                $scope.displayItems.push($scope.displayItem.displayConfig[0].propertyValue);
                                //$scope.displayItems = $scope.displayItem.displayConfig[0].propertyValue;
                                console.info($scope.displayItems);
                            });
}
                        break;

                    case 'people' :
                        for(var j=0; j<list.length; j++) {
                            getPeople(list[j]).fetch({}).$promise.then(
                                function (value) {
                                    $scope.displayItem = value.data[0];
                                    console.info($scope.displayItem);
                                    $scope.displayItems.push($scope.displayItem.displayConfig[0].propertyValue);
                                    //$scope.displayItems = $scope.displayItem.displayConfig[0].propertyValue;
                                    console.info($scope.displayItem.displayConfig[0].propertyValue);
                                    console.info($scope.displayItems);
                                });
                        }
                        break;
                }
            }

Short answer:

<ul data-ng-init="display(selecteditemslist,searchid)">
  <li class="tag" ng-repeat="list in displayItems track by $index"  ng-class="{selected: $index==selectedIndex}">
    <span class="tag-label">{{list}}</span><span class="tag-cross pointer" ng-click="Delete($index,selecteditemslist[$index],list,searchid)">x</span>
  </li>
</ul>

Long Answer:

Let's see how the directive ng-repeat works.

First of all:

var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
  var NG_REMOVED = '$$NG_REMOVED';
  var ngRepeatMinErr = minErr('ngRepeat');

  ...

  return {
    restrict: 'A',
    multiElement: true,
    transclude: 'element',
    priority: 1000,
    terminal: true,
    $$tlb: true,
    compile: function ngRepeatCompile($element, $attr) {
      var expression = $attr.ngRepeat;

      ...
      var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);

      var lhs = match[1];
  var rhs = match[2]; <--- in this case `displayItems`
  var aliasAs = match[3];
  var trackByExp = match[4];

After that $scope.$watchCollection(rhs, function ngRepeatAction(collection) { ... }) is being made in ngRepeatLink .

As a result if displayItems is empty, we will get html comment: <!-- ngRepeat: list in displayItems --> instead of what you probably supposed to see: <div ng-repeat="list in displayItems" ng-init="display(selecteditemslist,searchid)"> .

That's why ng-init doesn't initialize :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM