2016-05-10 47 views
0

这是我的局部视图 -

<form action="" ng-controller="Ctrl as forgot" name="forgotForm" ng-submit="forgotForm.$valid && forgot.sendPost()"> 
     <div class="form-group row"> 
      <label for="email" class="col-sm-2 form-control-label">Email address</label> 
      <div class="col-sm-9"> 
       <input type="email" ng-model="forgot.emailData" class="form-control" id="email" name="email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$" placeholder="Email Address" required/> 
      </div> 
     </div> 

     <div class="form-group row"> 
      <div> 
       <button class="btn btn-info submitButton">Submit</button> 
      </div> 
     </div> 
    </form> 

这是我的控制器的方法 -

self.sendPost = function() { 
     console.log(self.emailData); //the value I entered in the input box 
     $http.get("https://[email protected]") 
      .success(function(data, response, headers, config) { 
       console.log("Success Data " + response); 
      }) 

      .error(function(data, response, headers, config) { 
       console.debug("Returned Data "); 
      }); 
    }; 

例如,如果我输入的值[email protected]在输入框中,我看到我在表单中输入的实际值已发送到api(self.emailData的值)。我得到的200

下面的响应是我有问题,

1)为什么当我明确规定要发送的电子邮件地址self.emailData被发送到是[email protected]

2)由于我得到200的回应,为什么成功回调不被解雇。

回答

0

不推荐使用成功和错误。尝试重构您的代码是这样的:

self.sendPost = function() { 
    console.log(self.emailData); //the value I entered in the input box 
    $http.get("https://[email protected]") 
    .then(function successCallback(response) { 
     console.log(response) 
    }, function errorCallback(response) { 
     console.debug(response); 
    }); 
}; 
0

首先提示: 请不要在控制器的业务逻辑。将其移动到服务中并在服务器中注入服务。 按照您的答案尝试使用然后与成功和错误处理程序。给它一个例子beloe

$http.get(URL).then(successCallback, errorCallback); 

function successCallback(){ 

} 

function errorCallback(){ 

} 

希望这有一些帮助。

快乐学习:)

相关问题