簡體   English   中英

ng-submit不適用於這種情況

[英]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;
  });
 };
});

請讓我知道我要去哪里了。 這是我的第一個應用程序。 對不起任何愚蠢的錯誤:)

您需要將Submit函數綁定到scope屬性,因為普通函數,變量無法在html中訪問

喜歡

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

這是更新的朋克

它應該像下面。

$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,請從這里學習...

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