简体   繁体   English

设置变量时,Angular Js ng-init在ng-repeat内部无法正常工作

[英]Angular Js ng-init is not working properly inside ng-repeat while setting a variable

I have a ng-repeat where i am checking a if condition and at the end of each repeation of loop i am setting a variable to a value from ng-repeat 我有一个ng-repeat,我在其中检查if条件,在每次循环重复结束时,我都将变量设置为ng-repeat的值

Here is my variable which i want to set inside the ng-repeat 这是我要在ng-repeat中设置的变量

$scope.prvSid = "";

Here is my ng-repeat code 这是我的ng-repeat代码

<ul class="chats" ng-repeat="chatData in chatboxData">
    <li ng-class="{ 'out': chatData.sender_id == user_id , 'in': chatData.sender_id != user_id }">
         {{   prvSid }}
         {{   chatData.sender_id }}
        <span ng-if=" prvSid != chatData.sender_id ">
            <img class="avatar" alt="" src="{{ url('default/img/user_default_logo.jpg') }}" />
        </span>
        <div class="message">
            <span class="body"> {{ chatData.message }} </span>
        </div>
        <span ng-init="prvSid = chatData.sender_id"></span>
        {{   prvSid }}
    </li>
</ul>

The problem which i am facing here is that whenever i print these values inside the ng-repeat then prvSid and chatData.sender_id is printing the same id the value of chatData.sender_id even for the first iteration and that's why this 我在这里面临的问题是,每当我在ng-repeat中打印这些值时,prvSid和chatData.sender_id就将相同的id与chatData.sender_id的值进行打印,即使是第一次迭代,这就是为什么

<span ng-if=" prvSid != chatData.sender_id ">

condition is not working and my image is not displaying because for the first iteration the condition should be true because prvSid is "" and chatData.sender_id has some id in it 条件不起作用并且我的图像没有显示,因为对于第一次迭代,条件应该为true,因为prvSid为""并且chatData.sender_id中包含一些ID

The purpose of this is to Henry Zou the purpose is to not show profile picture for two or more messages submitted by same user (if the sender_id is same)Then dont display the profile image.When the new message comes from another sender which means sender_id is different then show the profile image 这样做的目的是为了让Henry Zou不再显示同一用户提交的两条或多条消息的个人资料图片(如果sender_id相同),则不显示个人资料图片。当新消息来自另一个发件人时,这意味着sender_id然后显示个人资料图片是不同的

at first the prvSid will be null so it will show the image because condition will not match at the end of each iteration i will set the prvSid to the current iteration sender_id to match this prv_id with sender_id in the next iteration 首先,prvSid将为null,因此它将显示图像,因为条件将在每次迭代结束时都不匹配。我将把prvSid设置为当前迭代sender_id,以便在下一次迭代中将这个prv_id与sender_id匹配

i am also getting messages after two seconds and then i am adding the new records in the chatboxdata if they dont exist in it 两秒钟后我也收到消息,然后我将新记录添加到chatboxdata中(如果它们中不存在)

$scope.getlasttwosecMsgs = function(user_id_chat,chat_id,user,chatboxData,is_new) {
      $http.get(url+'/getlasttwosecMsgs/'+chat_id).success(function(data){
        angular.forEach(data, function(value, key) {
          var exists = $scope.containsObject(value,chatboxData);
          if (!exists) {
            $scope.chatboxData.push(value);
            console.log($scope.chatboxData);
          };
        });
      });
    }
$scope.containsObject = function(obj, list) {
        var i;
        for (i = 0; i < list.length; i++) {
            if (angular.equals(list[i], obj)) {
                return true;
            }
        }

        return false;
    };

This new code snippet will check current userID against a list of users in the chatbox. 这个新的代码片段将根据聊天框中的用户列表检查当前的userID。 For all users that's not the current user, show content. 对于不是当前用户的所有用户,请显示内容。

approach #1 (preferred) 方法1(首选)

 angular .module('app', []) .controller('myCtrl', function(){ var vm = this; var prvSid = null; vm.chatboxData = [{id:1},{id:1},{id:2},{id:3}]; vm.chatboxData.forEach(function(chatbox){ if(prvSid !== chatbox.id){ chatbox.showIcon = true; } prvSid = chatbox.id; }); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script> <div ng-app="app"> <div ng-controller="myCtrl as vm"> <ul class="chats" ng-repeat="chatData in vm.chatboxData"> <li> <span ng-if="chatData.showIcon "> ICON </span> {{::chatData.id}} </li> </ul> </div> </div> 

approach #2 方法2

 angular .module('app', []) .controller('myCtrl', function(){ var vm = this; vm.prvSid = null; vm.chatboxData = [{id:1},{id:1},{id:2},{id:3}]; }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script> <div ng-app="app"> <div ng-controller="myCtrl as vm"> <ul class="chats" ng-repeat="chatData in vm.chatboxData"> <li> <span ng-if="chatData.showIcon "> ICON </span> {{::vm.prvSid}} {{::chatData.id}} <span ng-init="chatData.showIcon = (vm.prvSid !== chatData.id)"></span> <span ng-init=" vm.prvSid = chatData.id"></span> </li> </ul> </div> </div> 

approach #3 (without using controllerAs syntax) 方法3(不使用controllerAs语法)

 angular .module('app', []) .controller('myCtrl', function($scope){ var prvSid = null; $scope.chatboxData = [{id:1},{id:1},{id:2},{id:3}]; $scope.chatboxData.forEach(function(chatbox){ if(prvSid !== chatbox.id){ chatbox.showIcon = true; } prvSid = chatbox.id; }); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.js"></script> <div ng-app="app"> <div ng-controller="myCtrl"> <ul class="chats" ng-repeat="chatData in chatboxData"> <li> <span ng-if="chatData.showIcon "> ICON </span> {{::chatData.id}} </li> </ul> </div> </div> 

OLD ANSWER 老答案

In the video AngularJS MTV Meetup: Best Practices (2012/12/11), Miško explains "..if you use ng-model there has to be a dot somewhere. If you don't have a dot, you're doing it wrong.." 在视频AngularJS MTV聚会:最佳实践(2012/12/11)中,Miško解释说:“ ..如果您使用ng-model,则必须在某处有一个点。如果没有点,那么您就在做错误..”

ng-repeat creates an inherited scope (think javascript prototype) for each item. ng-repeat为每个项目创建一个继承的作用域 (例如javascript原型)。

This code below will create $scope.prvSid for each item in the array and the value will always be chatData.sender_id. 下面的代码将为数组中的每个项目创建$ scope.prvSid,其值始终为chatData.sender_id。

<span ng-init="prvSid = chatData.sender_id"></span>

If you meant it to only have 1 instances of prvSid id, then what you'll need to do is initialize prvSid variable at the parent scope first (or use the dot(.) rule) 如果您打算只包含1个prvSid id实例,那么您需要做的是首先在父范围内初始化prvSid变量(或使用dot(。)规则)

<div ng-init="prvSid = null"> <!-- initializing prvSid id @ parent scope -->
<ul class="chats" ng-repeat="chatData in chatboxData">
    <li ng-class="{ 'out': chatData.sender_id == user_id , 'in': chatData.sender_id != user_id }">
         {{   prvSid }}
         {{   chatData.sender_id }}
        <span ng-if=" prvSid != chatData.sender_id ">
            <img class="avatar" alt="" src="{{ url('default/img/user_default_logo.jpg') }}" />
        </span>
        <div class="message">
            <span class="body"> {{ chatData.message }} </span>
        </div>
<!-- this will now update the parent scope's prvSid instead of creating a new one for each item in the array -->
        <span ng-init="prvSid = chatData.sender_id"></span>
        {{   prvSid }}
    </li>
</ul>
</div>

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

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