简体   繁体   English

ng-submit不适用于这种情况

[英]ng-submit not working for this plunk

I am new to angular js and was building my first app to display users while hitting the github API. 我是angular js的新手,正在构建我的第一个应用程序以在点击github API时显示用户。

here's the plunk I was working on : http://plnkr.co/edit/bJxijtHV4kBmJ3heMxA9?p=preview 这是我正在研究的小品: 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 code: 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;
  });
 };
});

Please let me know where I am going wrong. 请让我知道我要去哪里了。 This is my first app. 这是我的第一个应用程序。 Excuse any silly mistakes :) 对不起任何愚蠢的错误:)

you needs to bind the submit function to scope property, because plain functions, variables are not accessible with in the html 您需要将Submit函数绑定到scope属性,因为普通函数,变量无法在html中访问

like 喜欢

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

here is the updated Punker 这是更新的朋克

it should be like following. 它应该像下面。

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

First of all try to learn how to write function in angularJS controllers. 首先,尝试学习如何在angularJS控制器中编写函数。 You are defining search as variable object not a function. 您正在将搜索定义为变量对象而不是函数。

Try this - 尝试这个 -

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

If you are new to angularJS learn from here...... 如果您不熟悉angularJS,请从这里学习...

http://stackoverflow.com/questions/11063673/whats-the-most-concise-way-to-read-query-parameters-in-angularjs http://stackoverflow.com/questions/11063673/whats-the-most-concise-way-to-read-query-parameters-in-angularjs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM