gpt4 book ai didi

javascript - 为什么 ng-submit 没有触发 $scope.submit 函数?

转载 作者:行者123 更新时间:2023-11-27 23:55:22 29 4
gpt4 key购买 nike

我的 App.js 包含以下代码:

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

我有 githubAppController 和以下代码:

app.controller('githubController', function ($scope, $http) {
$scope.submit = function () {
var promise = $http.get('https://api.github.com/users/odetocode');
promise.then(onUserGet);

var onUserGet = function (response) {
$scope.user = response.data;
};
};
});

在我的 page.html 中,我有以下代码:

!DOCTYPE html>
<html >
<head>
<title></title>
<meta charset="utf-8" />
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<link href="Content/custom.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="Scripts/jquery.validate.min.js"></script>
<script src="Scripts/App.js"></script>
<script src="Scripts/githubController.js"></script>
</head>
<body ng-app="githubApp">
<div ng-controller="githubController">
<form class="form-horizontal" ng-submit="submit()">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">

<input type="submit" id="submit" class="btn btn-primary" value="Submit" />
<input type="reset" id="submit" class="btn btn-primary" value="Reset" />
</div>
</div>

</form>

<div class="col-sm-5">
<label>{{user.name}}</label>
</div>
<div class="col-sm-7">
<a ng-href="{{user.blog}}">{{user.company}}</a>
</div>

<div>
<img ng-src="{{user.avatar_url}}" />
</div>
</div>

为什么 ng-submit 没有触发 $scope.submit 函数?我做错了什么吗?

var app = angular.module('githubApp', []);
app.controller('githubController', function($scope, $http) {
$scope.submit = function() {
var promise = $http.get('https://api.github.com/users/odetocode');
promise.then(onUserGet);

var onUserGet = function(response) {
$scope.user = response.data;
};

};
});
<!DOCTYPE html>
<html>

<head>
<title></title>
<meta charset="utf-8" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>

<body ng-app="githubApp">
<div ng-controller="githubController">
<form class="form-horizontal" ng-submit="submit()">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">

<input type="submit" id="submit" class="btn btn-primary" value="Submit" />
<input type="reset" id="submit" class="btn btn-primary" value="Reset" />
</div>
</div>
</form>
<div class="col-sm-5">
<label>{{user.name}}</label>
</div>
<div class="col-sm-7">
<a ng-href="{{user.blog}}">{{user.company}}</a>
</div>

<div>
<img ng-src="{{user.avatar_url}}" />
</div>
</div>

谢谢。

最佳答案

var promise = $http.get('https://api.github.com/users/odetocode');
promise.then(onUserGet);

var onUserGet = function (response) {
$scope.user = response.data;
};

onUserGet 此时尚未定义。在定义它之前先调用它。这是行不通的。

因此,提交触发(与您的想法相反)。但由于它已损坏,您不会观察到所需的副作用。

关于javascript - 为什么 ng-submit 没有触发 $scope.submit 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32308210/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com