0

I am new in Angular JS and learning it. I have a div and i want load data on startup with controller but i want reload it again when data has a new record.

index.html

<div id="third-content" ng-controller="IndexCtrl">
    <table id="table-class-detail" class="table table-atd table-responsive" ng-init="reloadElement('page001')">
    <tbody><tr><td>Loading...</td></tr></tbody>
    </table>
</div>

Angularjs Module :

var app = angular.module('app',['ngRoute','controller'])

.config(function($routeProvider){
    $routeProvider
    .when('/home',{
        controller: 'AtdCtrl'
    })
    .otherwise({
        //templateUrl: 'temp/404.html'
    })
})

controller :

angular.module('controller',[])

.controller('IndexCtrl', ['$scope', '$http', '$log', function($scope, $http, $log) {

    $scope.getCurrentClass = function($params){
        $interval(function(){
          $http.post('./reloadpage.php',{'temp':$params})
            .success(function(data){
                $("#table-class-detail tbody").html("<tr><td>Name:</td><td>"+data.name+"</td></tr> <tr><td>Code:</td><td>"+data.code+"</td></tr>");
            })
            .error(function(error){
                $log.error(error);
            })
        },1000);
    }
}])

reloadpage.php

$data = json_decode(file_get_contents("php://input"));
$temp = $data->temp;
$records = array();
$result = $this->currentClass($temp);
if(!empty($result)){
    $records = array('name' => $result->name,'code' => $result->code);
}else $records = null;
echo json_encode($records);

I follow tutorial form this site it's not working, i got an error ReferenceError: $interval is not defined

Can anyone help me ? How to reload/refresh element on table-class-detail tbody

3
  • Try to include your code with $interval with it so that we can see the problem clearer. Commented Dec 16, 2015 at 3:19
  • The question has been updated Commented Dec 16, 2015 at 3:25
  • Thanks. I have also updated the answer below Commented Dec 16, 2015 at 3:29

3 Answers 3

2

As Park said, you need to inject $interval in your controller or provider, but additionally, you shouldn't use jQuery to manipulate the DOM like that.

You should rather put something like this in your html:

<tbody ng-if="loading">
    <tr>
        <td>Loading...</td>
    </tr>
</tbody>
<tbody ng-if="!loading">
    <tr>
        <td>{{ name }}</td>
    </tr>
    <tr>
        <td>{{ code }}</td>
    </tr>
</tbody>

And something like this in your controller:

$scope.loading = true;
$scope.name = null;
$scope.code = null;

$scope.getCurrentClass = function($params){
    $http.post('./reloadpage.php', {'temp': $params})
    .success(function(data){
        $scope.name = data.name;
        $scope.code = data.code;
        $scope.loading = false;
    })
    .error(function(error){
        $log.error(error);
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

Upvote for mentioning usage of jQuery in controller
1

Since the error mentioned $interval is not defined, it hints that you forgot to inject $interval into your controller/service. Something like this:

angular
  .module('controller',[])
  .controller('IndexCtrl', [
    '$scope', '$http', '$log', '$interval', // inject $interval here...
    function($scope, $http, $log, $interval) { // and here...

      $scope.getCurrentClass = function($params) {
        $interval(function() { // ... so that you can use it here
          $http
            .post('./reloadpage.php',{
              'temp': $params
            })
            .success(function(data) {
              $("#table-class-detail tbody").html("<tr><td>Name:</td><td>"+data.name+"</td></tr> <tr><td>Code:</td><td>"+data.code+"</td></tr>");
            })
            .error(function(error){
              $log.error(error);
            })
          }, 1000);
        }
      }])

PS: When you have finished the tutorial, pay a visit to John Papa's style guide on best practices on AngularJS :)

7 Comments

still have some error ReferenceError: $interval is not defined
@FebryFairuz did you inject it properly? It should be injected in the function too
i inject it, but now i got error like this TypeError: $interval is not a function
@FebryFairuz that's strange. Check my updated answer for a clearer picture (you have to inject into two places). Otherwise, could it be $interval is also being used in other controller?
i follow your new update, there's no error but does not display any thing. *blank
|
0

Somewhere in the controller '$interval' parameter must be used for which you have to pass $interval into your controller angular.module('myCtrl', function($scope, $interval)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.