如何解决控制器未从工厂服务接收数据
你们是否发现我的工厂服务将数据发送到控制器2或控制器1将数据发送到工厂服务有什么问题?因为如果我尝试将数据记录在$ scope。$ watch内,则它在控制台上什么也没有显示,甚至没有定义,或者似乎从未执行过控制器2。但是当我在$ scope。$ watch之外记录随机字符串时,在控制台中显示它。我不为什么$ scope。$ watch在执行时不起作用(这是我的推测)。我非常感谢社区的帮助。
意图: 我想将请求的数据从控制器1共享到控制器2。它将在单击时执行。然后,发送到控制器2的数据将在html输入字段中使用。
工厂服务:
ReferenceError: window is not defined
at flatpickr (D:\expenditure\node_modules\flatpickr\dist\flatpickr.js:2618:31)
at router.get (D:\expenditure\routes\index.js:12:22)
at Layer.handle [as handle_request] (D:\expenditure\node_modules\express\lib\router\layer.js:95:5)
at next (D:\expenditure\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\expenditure\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\expenditure\node_modules\express\lib\router\layer.js:95:5)
at D:\expenditure\node_modules\express\lib\router\index.js:281:22
at Function.process_params (D:\expenditure\node_modules\express\lib\router\index.js:335:12)
at next (D:\expenditure\node_modules\express\lib\router\index.js:275:10)
at Function.handle (D:\expenditure\node_modules\express\lib\router\index.js:174:3)
at router (D:\expenditure\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (D:\expenditure\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\expenditure\node_modules\express\lib\router\index.js:317:13)
at D:\expenditure\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\expenditure\node_modules\express\lib\router\index.js:335:12)
at next (D:\expenditure\node_modules\express\lib\router\index.js:275:10)
控制器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;
});
控制器2:
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) {
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
});
});
});
HTML:
app.controller('editProfliesController',profilingFactory) {
$scope.$watch(function (scope) {
return scope.myVar = profilingFactory.getModel('proFactor')
},function (newVal,oldVal) {
if (newVal !== oldVal)
console.log(newVal)
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。