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

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

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

最佳答案

简短回答:

<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为空,我们将得到html注释:<!-- ngRepeat: list in displayItems -->而不是您可能应该看到的:<div ng-repeat="list in displayItems" ng-init="display(selecteditemslist,searchid)"> .

这就是为什么ng-init不初始化:)

关于javascript - 如何将值作为 ng-init 的参数传递给指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30103887/

相关文章:

javascript - 切换到另一个选项卡时如何停止滚动滑动?

javascript - jquery 在类的下一个实例中显示 ajax 响应

javascript - php 中的两个操作两个提交按钮?

jquery - 如何将数据推送到数组,当循环对象时

javascript - 在一个对象中返回两个不同的返回值

javascript - html2canvas 捕获 morris.js 图表

javascript - 将字符串中的特定单词更改为粗体

javascript - 如何禁用 ng-template-loader 查看图像源?

angularjs - 无法将范围传递给 Angular 形对话框

javascript - 如何从javascript中的url中删除查询字符串数组