1

I'd like to init a variable using AngularJS in my template... It seems not working.

Template :

<div class="eventList" ng-controller="EventOfCategoryController">
  <input type="hidden" ng-init="categoryId = <%=id%>" />
</div>

Js :

app.controller("EventOfCategoryController", function($scope, $http, $filter) {
  console.log($scope.categoryId); //undefined
  $http.get('/category/'+$scope.categoryId).success(function(data, status, headers, config) {
      // my functions
  });
});
2
  • Better to put the initialization code inside the controller. Commented Nov 25, 2015 at 9:56
  • I encountered the same issue and noticed, angular takes some time to init values. ie, you are accessing that value, before it is set. For testing, you might set a timeout and check you get that value. Commented Nov 25, 2015 at 10:00

4 Answers 4

2

You are trying to read the set value before Angular is done assigning.

app.controller("EventOfCategoryController", function($scope, $http, $filter) {
  $timeout(function(){
        console.log($scope.categoryId);
    },1000);
  $http.get('/category/'+$scope.categoryId).success(function(data, status, headers, config) {
      // my functions
  });
});

Ideally you should use $watch as bellow if you need to use ng-init at all:

app.controller("EventOfCategoryController", function($scope, $http, $filter) {
    console.log('test');
    $scope.$watch("categoryId", function(){
        console.log($scope.categoryId);
$http.get('/category/'+$scope.categoryId).success(function(data, status, headers, config) {
      // my functions
  });
    });
});

You can also use a method in you ng-init as follows. This will reduce watch load and increase performance.

app.controller("EventOfCategoryController", function($scope, $http, $filter) {
        console.log('test');
        $scope.init = function(categoryId){
            console.log(categoryId);
    $http.get('/category/'+categoryId).success(function(data, status, headers, config) {
          // my functions
      });
        });
    });

And ng-int as

<input type="hidden" ng-init="init(<%=id%>)" />
Sign up to request clarification or add additional context in comments.

1 Comment

@ParthaSarathiGhosh I tryed to use your code for my problem too but its not working. Could you help me out with my problem too? :) 40919155
0

you shoud init the variable in your controller:

app.controller("EventOfCategoryController", function($scope, $http, $filter) {
  $scope.categoryId = 7;  
  console.log($scope.categoryId); //undefined
  $http.get('/category/'+$scope.categoryId).success(function(data, status, headers, config) {
      // my functions
  });
});

And

<input type="hidden" ng-model="categoryId">

1 Comment

I can not doing that because id is send from the server :/ See edit in the template..
0

The controller(EventOfCategoryController) is being created before the categoryId variable is added to the $scope object.

$scope.$watch('categoryId', function () {
    console.log($scope.categoryId); 
});

Comments

0

If you wrap console.log in a setTimeout and utilise a $scope.$apply, categoryId will become available:

    setTimeout(function () {
        $scope.$apply(function () {
            console.log($scope.categoryId); // 7
        });
    }, 0);

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.