范围将值更新为变量

时间:2016-10-09 14:17:56

标签: javascript angularjs ionic-framework mailgun

我有这个:

$scope.data = {
    'a' : 0,
    'b' : 0,
    'c' : 0
}

...以及每个的值:

<input toggle ng-model="data.a" ng-true-value="10" ng-false-value="0" />

这是angularjs v1离子引用计算器的示例,每个字段都有一个值,我用{{data.a}} {{..}}显示正确的值,最后一个字段是所有{{data.a + data.b + data.c ....}}的总和 我需要在javascript变量上传递所有字段的实际值,以便将范围数据发送到电子邮件:

angular.module('app.mail', []).controller("MailgunController", function($scope, $http) {


    var mailgunUrl = "mydomain.com";
    var mailgunApiKey = window.btoa("api:key-XXXXXXXXXX");
    var recipient = "info@mydomain.com";
    var subject = "Quote";
    var message = "Results: " + $scope.data.a;


    $scope.send = function() {
        $http(
            {
                "method": "POST",
                "url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
                "headers": {

                    "Content-Type": "application/x-www-form-urlencoded",
                    "Authorization": "Basic " + mailgunApiKey
                },
                data: "from=" + "info@example.com" + "&to=" + recipient + "&subject=" + subject + "&html=" + message 
            }
        ).then(function(success) {
            console.log("SUCCESS " + JSON.stringify(success));
        }, function(error) {
            console.log("ERROR " + JSON.stringify(error));
        });
    };
});

问题是我收到的电子邮件的默认值为0:

$scope.data.a; // 0

如何将真实范围数据值转换为var消息?

谢谢

2 个答案:

答案 0 :(得分:0)

如果在返回结果时在服务器端使用web api控制器 将结果传递给新的{}对象。例如:

           [HttpPost]
            public HttpResponseMessage Insert(LookupViewModel model)
            {
                try
                {
                    EducationLookup result = AutoMapperGenericsHelper<LookupViewModel, EducationLookup>.ConvertToDBEntity(model);
                    this.Uow.EducationLookups.Add(result);
                    Uow.Commit(User.Id);
                   //Look here
                    return Request.CreateResponse(HttpStatusCode.OK, new{Result= result});
                }
                catch (DbEntityValidationException e)
                {
                    return Request.CreateResponse(HttpStatusCode.InternalServerError, CustomExceptionHandler.HandleDbEntityValidationException(e));
                }
                catch (Exception ex)
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest, ex.HResult.HandleCustomeErrorMessage(ex.Message));
                }

            }

在客户端获取成功函数的json格式。

答案 1 :(得分:0)

 $scope.send = function() {
        $http(
            {
                "method": "POST",
                "url": "https://api.mailgun.net/v3/" + mailgunUrl + "/messages",
                "headers": {

                    "Content-Type": "application/x-www-form-urlencoded",
                    "Authorization": "Basic " + mailgunApiKey
                },
                data: "from=" + "info@example.com" + "&to=" + recipient + "&subject=" + subject + "&html=" + message 
            }
        ).then(function(respose) {
            //Get server response in block! 
            $scope.data=respose;
            console.log("Send e-mail was successful");
        }, function(error) {
            console.log(error);
        });