[英]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.
但是我的ng-init函数甚至没有触发。 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. 让我们看看
ng-repeat
指令是如何工作的。
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
. 此后
$scope.$watchCollection(rhs, function ngRepeatAction(collection) { ... })
正在制作中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)">
. 结果,如果
displayItems
为空,我们将得到html注释: <!-- ngRepeat: list in displayItems -->
而不是您可能会看到的内容: <div ng-repeat="list in displayItems" ng-init="display(selecteditemslist,searchid)">
。
That's why ng-init
doesn't initialize :) 这就是
ng-init
不初始化的原因:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.