我想在单击按钮时调用一个函数,其中 HTTP 被调用以从 JSON 文件读取数据,当 HTML 加载来自 JSON 文件的数据时,以下代码工作正常 <ul>
但我想在按钮(继续按钮)单击上调用函数,我怎样才能实现这一点?因为当我编码http.get
时函数中的方法并调用按钮单击它不起作用。
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;
});
}]);
最佳答案
您需要将代码包装在函数内,并使用 ng-click 指令在单击时调用该函数。
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>
关于javascript - AngularJS : How to read data of JSON file on button click?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222588/