Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AngularJS中的单选按钮从api更新$http调用_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 使用AngularJS中的单选按钮从api更新$http调用

Javascript 使用AngularJS中的单选按钮从api更新$http调用,javascript,html,angularjs,Javascript,Html,Angularjs,我是个新手。正如标题所说,我如何通过过滤掉这里的“性别”来更新/或进行新调用,因此API只返回女性或男性。问题是什么都没发生,我不知道我做错了什么。请看下面我的代码 我的代码: var app = angular.module("app", []); app.controller("randomUserController", function($scope, $http) { $scope.$watch("gender", function () { fetch();

我是个新手。正如标题所说,我如何通过过滤掉这里的“性别”来更新/或进行新调用,因此API只返回女性或男性。问题是什么都没发生,我不知道我做错了什么。请看下面我的代码

我的代码:

var app = angular.module("app", []);

app.controller("randomUserController", function($scope, $http) {
    $scope.$watch("gender", function () {
        fetch();
    });

    $scope.users = [];
    $scope.gender = "male";

    function fetch() {
        $http({
            method: "GET",
            url: "https://randomuser.me/api/?results=1&?gender=" + $scope.gender
        }).then(
            function successCallback(response) {
                $scope.users.gender = response.data.results[0].gender;
                $scope.users.name =
                    response.data.results[0].name.first +
                    " " +
                    response.data.results[0].name.last;
                $scope.users.location =
                    response.data.results[0].location.city +
                    " " +
                    response.data.results[0].location.street +
                    " " +
                    response.data.results[0].location.state +
                    " " +
                    response.data.results[0].location.postcode;
                $scope.users.picture = response.data.results[0].picture.large;
            },
            function errorCallback(response) {
                console.log(response + " Some error occured");
            }
        );
        $scope.update = function (response) {
            $scope.gender = response.data.results[0].gender;
        };
    }
});
HTML:


{{users.gender}

“update”-方法或任何它被调用的方法,我希望它触发对API的新调用,并返回雌性或雄性。这是不是更接近正确?提前谢谢

我不明白你为什么说“什么都没发生”。这里面发生了很多事情。@georgeawg我解开了我自己的谜团。。。我仔细看了看,发现了问题所在,等等,还有第四点。无论如何,谢谢!:)
<div ng-controller='randomUserController'>
  <div class='radio_group' data-label='Male'>
    <input id='male' name='gender' ng-click='update(response)'
           ng-model-options="{updateOn: 'default blur'}"
           ng-model='gender' ng-required='!gender'
           type='radio' value='male'>
    <label for='male'></label>
  </div>
  <div class='radio_group' data-label='Female'>
    <input id='female' name='gender' ng-click='update(response)'
           ng-model-options="{updateOn: 'default blur'}"
           ng-model='gender' ng-required='!gender' type='radio'
           value='female'>
    <label for='female'></label>
{{users.gender}}
</div>