ng-repeat behaviour with $watch

I am newbie to AngularJs. I am using AngularJS as front end and Laravel As backend. I have a situation where I want page to be refreshed on ajax success (when user post data).

I have template index.blade.php like this :

@extends(layout)
 @section

 header page
  post page 
  main page 
  footer page 

@endsection

Now thing is that User is posting update from post page and on that success i want data to be refreshed which is on main page

function addpost ($scope, $http){

$scope.loadData = function(){
            $http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
            $http.post( baseurl+"alldata").success(function(data)
            {
              $scope.posts = data;
         });
             }


                     $scope.addComment = function(post){

        if("undefined" != post.hoot){
        // Angular AJAX call            
        $http({
        method : "POST",
        url :baseurl+ "url",
        data : "post="+post
        }).success(function(data){
             $scope.posts = data;//json response

        // here i want page to be refreshed or div refresh of sub-main    page 

            });`
            $scope.post = ' ';`
            }
            }
                     $scope.loadData();

}`

Now thing is that if i am posting data from other sub page and then showing it on other sub page. Iam calling addcomment to add posted data in model and then loaddata() to get all data from model for ng-repeat in a view.

if i am going to fire watch event . it will keep on refreshing model thus view data. Suppose i am working on comment section where we can like , dislike and comment on post. and i want to use ng-click event inside ng-repeat(which is refreshing view).

Test.controller('Testing',['$scope', '$http', function($scope,$http){           
         $scope.products = {};           

             $http.post( baseurl+"ajax/getall").success(function(data)
        { 
            $scope.products = data;

         });
             $scope.$watch("products", function(newValue, oldValue) {
 if (newValue === oldValue) { return; } // AKA first run
    $scope.products= newValue;
 });
              });



        $scope.Hello= function(){

            alert("hello");
        }      

    }]);

It will keep on refreshing page and thus don't click event inside ng-repeat which is ng-click. I have fired ng-click="Hello()" event but it doesn't fire.

Answers


You can register a listener to $scope.data in any part of the application that had a reference to scope. This way, everything will keep up to date when data is updated.

Like so:

$scope.$watch('data', callback)


You are thinking the jQuery way. You have to put that away and think data-binding and angularjs.

To do what you are asking is pretty easy if you think the angularjs way. First of all, you need the php page of yours to print out a html page with a {{updateMe}}

then in your code

.success(function(data){
  $scope.updateMe = data
  ......

Need Your Help

SIGSEGV error when using AES_ctr128_encrypt for sockets

c sockets encryption openssl

When i am trying to encrypt the data of the file using AES_ctr128_encrypt i am getting SIGBUS error. Here, I am using read() function to read the data from the file to the bufptr[32k] in 32k chunk...