【发布时间】:2017-05-12 07:54:56
【问题描述】:
我是 angularjs 的新手,使用 1.6.4 版本。我正在使用这个模块leon/angular-upload 来实现上传功能,缩小版本。在成功的上传请求上,服务器在 onSuccess(response) 函数上返回上传文件信息的 json 对象,如您在我的 user-registration.template.html 文件中所见。现在我需要将此 json 对象带到我的控制器,以便我可以将此信息保存在我的数据库中。下面是我的几行代码。
用户注册.template.html:
<form role="form">
<div class="form-group float-label-control">
<label>Name</label>
<input type="text" placeholder="Name" class="form-control" ng-model="model.user.name">
</div>
<!-- leon/angular-upload -->
<div upload-button
url="/user_uploads"
on-success="onSuccess(response)"
on-error="onError(response)">Upload</div>
<div class="text-center">
<button type="button" class="btn btn-default" ng-click="model.save(model.user)">Save</button>
</div>
</form>
我的组件“user-registration.component.js”:
(function(){
"use strict";
var module = angular.module(__appName);
function saveUser(user, $http){
var url = user.id > 0 ? __apiRoot + "/users/" + user.id : __apiRoot + "/users";
var dataObj = {
payload: JSON.stringify(user),
_method: "PUT"
}
return $http.post(url, dataObj);
}
function controller($http){
var model = this;
model.user = null;
model.save = function(user){
console.log(JSON.stringify(user));
saveUser(user, $http).then(function(response){
alert(response.data.msg);
});
}
}
module.component("userRegistration", {
templateUrl: "components/user-registration/user-registration.template.html",
bindings: {
value: "<"
},
controllerAs: "model",
controller: ["$http", controller]
});
}());
【问题讨论】:
标签: javascript angularjs json