简体   繁体   English

将jQuery表单提交转换为角度

[英]convert jquery form submission to angular

I'm trying to convert the following jquery into angular, but can't seem to get it to work. 我正在尝试将以下jquery转换为angular,但似乎无法使其正常工作。

// jquery
$('#formsubmit').click(function(ev){
  ev.preventDefault();
  $.ajax({
    url: "/url.json",
    type: "POST",
    data: {email: $("#emailinput").val()},
    dataType: "json",
    success: function(data){
        console.log(data);
    },
    error: function(data){
      console.log(data);
    }
  });
});

<!--html -->
<form action="/" method="post">
  <input type="text" id="emailinput" name="emailinput" />
  <input type="submit" id="formsubmit">
</form>

Here is my angular. 这是我的角度。 I put most of the logic into a controller, I'm not really sure if it's the best approach. 我将大多数逻辑放入控制器中,但我不确定这是否是最佳方法。 Would it be better to use a directive? 使用指令会更好吗?

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

app.controller('myCtrl', [
'$scope',
'$http',
function($scope, $http) {
  $scope.submitform = function() {
    $http({
      url: "/url.json",
      method: "POST",
      data: {email: $scope.email}
    }).success(function(data, status, headers, config) {
      $scope.data = data;
    }).error(function(data, status, headers, config) {
      $scope.status = status;
    });
  }
}]);

<!-- html -->
<div ng-controller="myCtrl">
  <form method="post" ng-submit="submitform()" novalidate>
    <input type="text" ng-model="email" required></input>
    <button type="button"></button>
  </form>
</div>

EDIT 编辑

Thanks for the help so far. 感谢你目前的帮助。 I adjusted a few things, but still having problems. 我做了一些调整,但仍然有问题。 When I click the button nothing seems to happen. 当我单击按钮时,似乎什么都没有发生。 Here's what I have so far. 到目前为止,这就是我所拥有的。

------html------ ------ HTML ------

<!-- putting everything in emailDir directive -->
<emailDir>
  <input type="text" ng-model="email"></input>
  <button ng-click="send">submit</button>        
</emailDir>

------factory file----- ------工厂文件------

var emailUser = angular.module("emailUsers", []);

emailUser.factory("emailUsers", [ "$http", "$q", function($http, $q){
  var fetch = function(theEmail){
    $http.post('/url', {email: theEmail})
        .success(function(data, status, headers, config) {
        $scope.data = data;
        console.log("success", data);
        }).error(function(data, status, headers, config) {
        $scope.status = status;
        console.log("error", status);
    });
  }
  return {
    fetch: fetch,
  }
}]);

-----directive------ - - -指示 - - -

var emailSubscribe = angular.module("emailsubscribe", []);

emailSubscribe.directive('emailDir', [function() {
  return {
    restrict: 'E',
    replace: true,
    controller: ['$scope', '$filter', '$attrs', 'i18n', 'emailUsers',   function($scope, $filter, $attrs, i18n, emailUsers) {
    $scope.email; 
    $scope.response = [];
    // when the user clicks the button with the ng-click="send", 
    // I want this to call the factory "emailUsers" fetch function function
    $scope.send = function() {
        emailUsers.fetch($scope.email).then(function(responses) {
            $scope.response = responses;
        });
      }
    }]
  };
}]);

--------dependencies------- -------- -------依赖

  // app.js file
  var dependencies = ["emailUsers", "emailsubscribe"]

I guess your sever side pick up the email by a variable String. 我想您的服务器端会通过变量String来接收电子邮件。 But when you transform to angular, you send the email capsulating in a json object of this format {name:value} . 但是,当您转换为angular时,您将发送封装在{name:value}格式的json对象中的电子邮件。 It is not a String. 它不是字符串。

If I were right, just modify to data: $scope.email , it would be fine. 如果我是对的,只需修改data: $scope.email ,就可以了。

Can you show more about your sever side implementation? 您能否显示更多有关服务器端实施的信息?

You should call $http service post function like so: 您应该像这样调用$http服务post函数:

$http.post('/url.json', {email: $scope.email})
  .success(function(data, status, headers, config) {
      $scope.data = data;
  }).error(function(data, status, headers, config) {
      $scope.status = status;
  });

And the <button> should be type submit , preferably with some text. <button>应型submit ,优选具有一些文本。

<button type="submit">go</button>

You can make the whole form a directive and use myCtrl as that directive's controller. 您可以使整个表单成为指令,并使用myCtrl作为该指令的控制器。 See AngularJS Directive documentation . 请参阅AngularJS指令文档

You are call on submit but not passing any values. 您被要求提交但未传递任何值。 so you need to pass the values to the scope. 因此您需要将值传递给范围。

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

app.controller('myCtrl', [
'$scope',
'$http',
function($scope, $http) {
  $scope.submitform = function(credentials) {
    $http({
      url: "/url.json",
      method: "POST",
      data: {email: credentials.email}
    }).success(function(data, status, headers, config) {
      $scope.data = data;
    }).error(function(data, status, headers, config) {
      $scope.status = status;
    });
  }
}]);

<!-- html -->
<div ng-controller="myCtrl">
  <form method="post" ng-submit="submitform(user)" novalidate>
    <input type="text" ng-model="user.email" required></input>
    <button type="submit"></button>
  </form>
</div>

I figured out what I was doing wrong. 我发现自己在做什么错。 Directive names can't be camelcase in angular. 指令名称不能以驼峰表示。 Replace emailDir with emaildir emaildir替换emailDir

-----directive------ - - -指示 - - -

var emailSubscribe = angular.module("emailsubscribe", []);

emailSubscribe.directive('emaildir', [function() {
  return {
    restrict: 'E',
    replace: true,
    controller: ['$scope', '$filter', '$attrs', 'i18n', 'emailUsers',   function($scope, $filter, $attrs, i18n, emailUsers) {
    $scope.email; 
    $scope.response = []; 
  function
    $scope.send = function() {
        emailUsers.fetch($scope.email).then(function(responses) {
            $scope.response = responses;
        });
      }
    }]
  };
}]);

<emaildir>
  <input type="text" ng-model="email"></input>
  <button ng-click="send">submit</button>        
</emaildir>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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