1

I am trying to display the clicked_user to the chat window when clicked. But this value is never updated on the chat window even though I see it in console.

Controller code snippet -

 $(document).on('click', '.chatwin', function (e)
    {
    $scope.clicked_user = $(e.target).text();
     console.log("USER "+ $scope.clicked_user);
                $('#chat_window_1').show();
            });

HTML Code

 <div class="popup-head">
  <div class="popup-head-left pull-left"   ><img src="assets/images/AshaLogo.jpg" alt="User Image" >{{clicked_user}}</div>
<div class="popup-head-right pull-right"  style="text-align: right;"><div id="circle_green"></div></div>
</div>

Another observation is that if I initialise this value outside the function e.g as below it works . Can you let me know what am i doing wrong here.

Controller code

$scope.clicked_user =“DUMMY”

 $(document).on('click', '.chatwin', function (e) {
                //$(this).parent().parent().parent().parent().remove();
                $scope.clicked_user = $(e.target).text();
                console.log("USER "+ $scope.clicked_user);
                $('#chat_window_1').show();
            });

2 Answers 2

2

Try like this:

$scope.$apply(function () {
    $scope.clicked_user = $(e.target).text();
});

$scope.$apply calls digest and notify angular that something has changed.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd even consider wrapping it in a $timeout callback function with no delay parameter, it will execute after the current digest is complete, avoiding calling apply while a digest is in progress.
0

Adding $scope.$apply(); as shown below did the trick .

$scope.clicked_user=[];
                $(document).on('click', '.chatwin', function (e) {
                //$(this).parent().parent().parent().parent().remove();
                $scope.clicked_user= $(e.target).text();
                $scope.$apply();
                console.log("USER "+ $scope.clicked_user);
                $('#chat_window_1').show();
            });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.