0

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 :)

4

3 回答 3

4

您需要将提交函数绑定到范围属性,因为在 html 中无法访问普通函数、变量

喜欢

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

这是更新的朋克

于 2015-04-09T09:34:59.343 回答
2

它应该如下所示。

$scope.Search = function(userName){
//rest of the code goes here
}
于 2015-04-09T09:38:52.393 回答
1

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

尝试这个 -

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

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

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

于 2015-04-09T09:43:16.427 回答