简体   繁体   中英

$scope variable in angular controller not getting displayed in HTML

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 eg 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();
            });

Try like this:

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

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

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();
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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