简体   繁体   English

如果angularjs中有多个项目,则ng-init不起作用,但是对单个项目起作用

[英]ng-init not working if multiple items in angularjs, but working for single item

I am new to stackoverflow, Sorry if this question is not proper. 我是stackoverflow的新手,如果这个问题不合适,我很抱歉。

I had declared a function in ng-init within the ng-repeat(session) to get the questions length by passing session id. 我在ng-repeat(会话)中的ng-init中声明了一个函数,通过传递会话ID来获得问题长度。

//This is the HTML Part
<tr ng-repeat="session in sessionslist" ng-init='getQuestionsofSession(accessors.getId(session))' >
<td>{{session.name}}</td>
<td>{{session.speakers.length}}</td>
<td><p>{{questions.length}}</p></td>
</tr>

//This is the ANGULAR Part
    $scope.getQuestionsofSession = function(sessionid){
        $http.get('/api/audiencepanel/getquestions/'+sessionid)
            .success(function(data){                
                $scope.questions= data;
            });
    };

//Getting sessions list
$scope.getsessions= function(){
$http.get("api/getsession/"+$stateParams.id).success(function(data){
$scope.sessionslist = data;
});

So if the session length is single, ng-init is working, but sessions length multiple it is not showing any data. 因此,如果会话长度为单个,则ng-init正在工作,但会话长度为多个,则不显示任何数据。 $scope.questions is not showing any data $ scope.questions未显示任何数据

By the way, Session table is different and Question table is different, questions are stored based on session id. 顺便说一下,会话表是不同的,问题表是不同的,问题是根据会话ID存储的。 So need questions data in session data based on session id 因此,需要根据会话ID在会话数据中提问数据

Is there any other way to get this worked? 有没有其他办法让这个工作?

Inside each ng repeat we have different scope. 在每个ng重复内部,我们有不同的范围。 So question object in sessionslist[0] is not same as sessionslist[1] 所以sessionslist[0] question对象与sessionslist[1]

Instead of using init you could do like this 而不是使用init你可以这样做

$scope.getsessions= function(){
   $http.get("api/getsession/"+$stateParams.id).success(function(data){
   angular.forEach(data, function(value, key){
      $http.get('/api/audiencepanel/getquestions/'+value.id)
            .success(function(data1){                
                value.questions= data1;
            });
   });
   $scope.sessionslist = data;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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