简体   繁体   中英

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.

here's the plunk I was working on : 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:

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

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. 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......

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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