如何将值作为ng-init的参数传递给指令

时间:2015-05-07 14:22:33

标签: javascript angularjs

我试图在加载页面时传递值列表。但我的ng-init功能甚至没有触发器。我的代码是什么问题。

<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>

到控制器中的功能

$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;
                }
            }

1 个答案:

答案 0 :(得分:0)

简答:

<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>

长答案:

让我们看看指令ng-repeat的工作原理。

首先:

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];

$scope.$watchCollection(rhs, function ngRepeatAction(collection) { ... })之后ngRepeatLink进行displayItems

如果<!-- ngRepeat: list in displayItems -->为空,我们会收到html评论:<div ng-repeat="list in displayItems" ng-init="display(selecteditemslist,searchid)">而不是您可能会看到的内容:ng-init

这就是mail.GetInspector().WordEditor 未初始化的原因:)