0

I'm trying to build a simple parse json example. I took the code from the following link : http://jsfiddle.net/mjaric/pJ5BR/

 <p>    Click <a ng-click="loadPeople()">here</a> to load data.</p>

All I want to do is instead of loading the data when clicking a button, I want it to load immediatly. What can I use instead of ng-click ? I've tried ng-init but it didn't work. Any idea? thanks

2 Answers 2

2

you can set a init function in your controller then call it int the controller after the init function definition. like below

var init = function(){
  //add your init code
  var httpRequest = $http({
        method: 'POST',
        url: '/echo/json/',
        data: mockDataForThisTest

    }).success(function(data, status) {
        $scope.people = data;
    });
}

init();

And the above sloution is work. OR u can try the ng-init like below

<div ng-controller="PeopleCtrl" ng-init="loadPeople()">

I think the second is the one u want : )

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

1 Comment

the ng-init is what I tried and that's exacly what I needed. I didn't notice I had error cause it couldn't find the JSON and that was the reso
0

If you want to immediately load the data you have to immediately call the loadPeople() function.
E.g. in the code you link to, you can add the following line at the end of your controller:

$scope.loadPeople();

See, also, this short demo.

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.