【发布时间】:2020-12-30 15:17:12
【问题描述】:
我试图将我的数据从控制器 1 传递到控制器 2,但是当我在控制器 2 中记录数据时,它返回一个未定义的值。我之所以使用 jquery 来请求数据是因为我使用的是数据表,我不知道如何使用 angularjs 和数据表框架进行事件绑定。我想知道根据控制器 1 向其发送数据,我的工厂服务实现是否正确,因为我认为这就是我现在的问题所在。数据未经过工厂服务。
工厂服务
app.factory('profilingFactory', function() {
var factoryData = {};
var factoryModel = {};
factoryData.setModel = function(key, value) {
factoryModel[key] = value;
}
factoryData.getModel = function(key) {
return factoryModel[key];
};
return factoryData;
});
控制器 1
app.controller('profilingController', function($scope, $http, $window, profilingFactory) {
$(document).ready(function() {
$scope.table = $('#table_profile').DataTable ({
"ajax": {
"url": "/getProfilesData",
},
"columns": [
{"data": "applicant_id"},
{"data": "application_type"},
{"data": "last_name"},
{"data": "middle_name"},
{"data": "first_name"},
{"data": "birth_date"},
{"data": "address"},
{"data": "sex"},
{"data": "date_applied"},
{
"data": "applicant_id",
"mRender": function (data, type, full) {
var editLink = '<button id="editFunc" class="btn btn-primary" type="button" data-id="' + data + '">Edit</button>';
//var editLink = '<a id="editFunc" class="editFunction" href="/editProfiles?applicant_id=' + data + '">Edit</a>';
var deleteLink = '<button class="btn btn-primary" id="deleteButtonFunc" type="button" data-id="'+ data +'">Delete</button>';
return (editLink + " " + " | " + " " + deleteLink);
}
}
]
});
$('body').on('click', '#editFunc', function(e) {
var clickedEdit = $(this);
var id = clickedEdit.attr("data-id");
var url = "/editProfiles?applicant_id=" + id;
var editData = {
editId: id
}
//$(location).attr('href', url);
$http.get('/getEditProfilesData/', {params: editData}).then(function(response) {
//this is orignally what I inteded. I only put a string to test if the service is working
profilingFactory.setModel('proFactor', response.data);
});
});
$scope.stringXample = "Yeh";
//$scope.editProfiles = appData;
$('body').on('click', '#deleteButtonFunc', function(e){
var clicked = $(this);
var id = clicked.attr("data-id");
var deleteData = {
deleteId: id
}
$http.get('/delProfileData/', {params: deleteData}).then(function(response) {
clicked.parents("tr").remove();
alert("User info successfully removed");
});
//Optional delete Row after
});
});
控制器 2
app.controller('editProfliesController', function($scope, $http, $window, profilingFactory) {
$scope.value = profilingFactory.getModel('proFactor');
console.log($scope.value);
});
HTML
<div class="row">
<h1 class="header-edit">Basic Information</h1>
<div class="col-md-3">
<div class="form-group">
<label for="pwd">Last Name:</label> <input type="text"
class="form-control" name="pwd" value="{{appData.last_name}}">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="email">First Name:</label> <input type="text"
class="form-control" name="email" value="{{appData.first_name}}">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="pwd">Middle Name:</label> <input type="text"
class="form-control" name="pwd">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="pwd">Birth:</label> <input type="date"
class="form-control" name="pwd" >
</div>
</div>
</div>
【问题讨论】:
-
这种行为可能是因为您在 Promise 中执行 setModel。看起来在您的承诺可以在 Controller 1 中解决之前,Controller 2 正在加载。为了确认这一点,您可以尝试将 setModel 移出 http.get 并查看它是否有效?
-
我尝试在 $http 中移出 setModel,但它仍然返回未定义的值。
标签: jquery angularjs datatables