【问题标题】:ng-submit not working for this plunkng-submit 不适用于这个 plunk
【发布时间】:2015-06-14 14:26:40
【问题描述】:

我是 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;
  });
 };
});

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

【问题讨论】:

    标签: angularjs ng-submit


    【解决方案1】:

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

    喜欢

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

    这里是updated Punker

    【讨论】:

      【解决方案2】:

      应该如下所示。

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

      【讨论】:

        【解决方案3】:

        首先尝试学习如何在 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-11-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-01-28
          • 2019-10-06
          • 1970-01-01
          相关资源
          最近更新 更多