0

I want to call a function on button click in which HTTP called to read data from JSON file , Following code is working fine when HTML loads the data from JSON file is loading on <ul> but I want to call function on button (Continue button) click , How can I achieve this? because when I encoded http.get method in a function and calls on button click it doesn't work .

HTML

 <label class="item">
              <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="submit"  ng-disabled="ForgotPasswordForm.$invalid" >Continue</button>

            </label>
    <ul>
      <li ng-repeat="x in loginData">
        {{ x.result +','+ x.status}}
      </li>
    </ul>

SignupCtrl.js

use strict';
angular.module('User').controller('SignupCtrl', ['$scope', '$http','$state', "SettingService", function($scope,$http, $state, SettingService) {

    /*LoginCtrl code will come here*/
    var vm = this;

 vm.signup = function(){
  $state.go('app.orderlist');
 };


 $http.get("forgotpassword.json").success(function (data) {
      $scope.loginData = data;

  });

}]);
4
  • it's bit hard to understand your requirement ,what you do mean by "Following code is working fine when HTML loads the data from JSON file is loading on <ul>" Commented Apr 5, 2017 at 5:37
  • I mean that The following code is working fine when the HTML page loads it reads data from json file and loads data in the list view But I want to do this on Button Click not on page load Commented Apr 5, 2017 at 5:39
  • Possible duplicate of How do i load json data only after a button is pressed in angularJS Commented Apr 5, 2017 at 5:43
  • answer is given in answer column ,reply me if you still did n't understand it Commented Apr 5, 2017 at 5:44

3 Answers 3

1

You need to wrap the code inside a function and call the function on click using the ng-click directive.

vm.getData = function(){
  $http.get("forgotpassword.json").success(function (data) {
      $scope.loginData = data;

  });
}

HTML:

  <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="submit" ng-click="vm.getData()"  ng-disabled="ForgotPasswordForm.$invalid" >Continue</button>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks one more query is How can I display data which I fetch from JSoN file in HTML?
Thnakyou So much
why the answer is removed?
0
vm.getData= function(){
$http.get("forgotpassword.json").success(function (data) {
  $scope.loginData = data;
 });
};

In html use

ng-click="vm.getData()"

Comments

0

you can modify your button like this

 <button class="button button-block button-positive" id="ForgotPasswordSubmit" type="button"  ng-disabled="ForgotPasswordForm.$invalid" 
ng-click="functionForLoadData()">Continue</button>

3 Comments

Thanks one more query is How can I display data which I fetch from JSON file in HTML?
<h1 ng-repeat="x in DataArrayYouFetched">{{x}}</h1>
function is not calling on button click

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.