[英]how to pass value to directive as a parameter of ng-init
页面加载时,我试图传递值列表。 但是我的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;
}
}
简短答案:
<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
不初始化的原因:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.