ng-submit 不适用于此 plunk

ng-submit not working for this plunk

我是 angular js 的新手,正在构建我的第一个应用程序以在点击 github API 时显示用户。

这是我正在处理的问题: http://plnkr.co/edit/bJxijtHV4kBmJ3heMxA9?p=preview

<form name="searchUser" ng-submit="Search(userName)">
  <input type="search" placeholder="User to find" ng-model="userName"/>
  <input type="submit" value="Search" />
</form>

Javascript代码:

var app = angular.module('myApp', []);
app.controller('MainController', function($scope, $http) {
var Search = function(userName) {
$http.get('https://api.github.com/users/' + userName)
  .then(function(response) {
    $scope.person = response.data;
  });
 };
});

请让我知道哪里出错了。这是我的第一个应用程序。请原谅任何愚蠢的错误:)

您需要将提交函数绑定到作用域 属性,因为在 html

中无法访问普通函数和变量

喜欢

$scope.Search = function(userName) {
   $http.get('https://api.github.com/users/' + userName)
       .then(function(response) {
           $scope.person = response.data;
       });
    };
});

这里是updated Punker

应该是这样的

$scope.Search = function(userName){
//rest of the code goes here
}

首先尝试学习如何在 angularJS 控制器中编写函数。您将搜索定义为变量对象而不是函数。

试试这个 -

$scope.search = function(userName) {
$http.get('https://api.github.com/users/' + userName)
  .then(function(response) {
    $scope.person = response.data;
  });
};

如果您是angularJS的新手,请从这里学习……