为什么ng-submit没有触发$ scope.submit函数?

时间:2015-08-31 09:39:22

标签: javascript angularjs

我的App.js包含以下代码:

var app = angular.module('githubApp', []);

我有githubAppController,代码如下:

app.controller('githubController', function ($scope, $http) {
    $scope.submit = function () {
        var promise = $http.get('https://api.github.com/users/odetocode');
        promise.then(onUserGet);

        var onUserGet = function (response) {
            $scope.user = response.data;
        };
    };
});

在我的page.html中,我有以下代码:

!DOCTYPE html>
<html >
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <link href="Content/custom.css" rel="stylesheet" />
    <script src="Scripts/jquery-1.9.1.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="Scripts/jquery.validate.min.js"></script>
    <script src="Scripts/App.js"></script>
    <script src="Scripts/githubController.js"></script>
</head>
<body ng-app="githubApp">
    <div ng-controller="githubController">
        <form class="form-horizontal" ng-submit="submit()">
 <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">

                        <input type="submit" id="submit" class="btn btn-primary" value="Submit" />
                        <input type="reset" id="submit" class="btn btn-primary" value="Reset" />
                    </div>
                </div>

        </form>

<div class="col-sm-5">
            <label>{{user.name}}</label>
        </div>
        <div class="col-sm-7">
            <a ng-href="{{user.blog}}">{{user.company}}</a>
        </div>

        <div>
            <img ng-src="{{user.avatar_url}}" />
        </div>
    </div>

为什么ng-submit没有触发$ scope.submit函数?我在做什么不对劲?

var app = angular.module('githubApp', []);
app.controller('githubController', function($scope, $http) {
  $scope.submit = function() {
    var promise = $http.get('https://api.github.com/users/odetocode');
    promise.then(onUserGet);

    var onUserGet = function(response) {
      $scope.user = response.data;
    };

  };
});
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <meta charset="utf-8" />
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
</head>

<body ng-app="githubApp">
  <div ng-controller="githubController">
    <form class="form-horizontal" ng-submit="submit()">
      <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">

          <input type="submit" id="submit" class="btn btn-primary" value="Submit" />
          <input type="reset" id="submit" class="btn btn-primary" value="Reset" />
        </div>
      </div>
    </form>
    <div class="col-sm-5">
      <label>{{user.name}}</label>
    </div>
    <div class="col-sm-7">
      <a ng-href="{{user.blog}}">{{user.company}}</a>
    </div>

    <div>
      <img ng-src="{{user.avatar_url}}" />
    </div>
  </div>

谢谢。

3 个答案:

答案 0 :(得分:1)

var promise = $http.get('https://api.github.com/users/odetocode');
promise.then(onUserGet);

var onUserGet = function (response) {
  $scope.user = response.data;
};
目前尚未定义

onUserGet。在定义它之前调用它。这不起作用。

因此,submit 触发(与您的想法相反)。但是因为它被打破了,你没有观察到所需的副作用。

答案 1 :(得分:0)

使用以下代码。

$scope.submit = function() {

        var promise = $http.get('https://api.github.com/users/odetocode');
        promise.then(function(response) {
          $scope.user = response.data;
        });

答案 2 :(得分:0)

你可以在你的控制器中使用这样的东西,

http://myUrl:9090/my/path/products?propertyKey=productname&propertyValue=wo*