I want to call editopenComponentModal in my other method its show error angular.js:13920 TypeError: Cannot read property 'editopenComponentModal' of undefined
EditCurrentJob(job) {
this.$http.put(properties.job_path + "/"+job.job_id).then(function successCallback(response) {
console.log(response.data);
this.current_job = response.data;
this.editopenComponentModal();
},
function errorCallback(response) {
});
}
editopenComponentModal() {
var modalInstance = this.$uibModal.open({
animation: this.animationsEnabled,
template: require('./Report/editsubmittedinformation.html'),
scope: this.$scope,
size: 'lg'
});
this.$scope.modalInstance = modalInstance;
return modalInstance.result;
}
Use the function references for that purpose, jopefully this will help you out.
var vm = this;
vm.editopenComponentModal = editopenComponentModal;
function EditCurrentJob(job) {
$http.put(properties.job_path + "/"+job.job_id).then(function successCallback(response) {
console.log(response.data);
vm.current_job = response.data;
vm.editopenComponentModal();
},
function errorCallback(response) {
});
}
function editopenComponentModal() {
var modalInstance = this.$uibModal.open({
animation: this.animationsEnabled,
template: require('./Report/editsubmittedinformation.html'),
scope: this.$scope,
size: 'lg'
});
this.$scope.modalInstance = modalInstance;
return modalInstance.result;
}
If you want to open a modal after $http.put request then use.
$('#success').modal();
here success is the id.
Add var that = this above the this.$http.put(
Then change:
this.current_job = response.data;
this.editopenComponentModal();
To:
that.current_job = response.data;
that.editopenComponentModal();
Explanation:
this inside the callback has different context, so you need to save the desired this to a variable which can be used there.
Here you can read the better explanation:
How to access the correct `this` context inside a callback?
Related
I am trying to set a Boolean property on an element in my array object, which I have in my scope.
In the code given below, when I try to set tasks[id].deleted = true, I get the following error.
angular.js:12798 TypeError: Cannot set property 'deleted' of undefined
at Scope.$scope.delete (main.js:54)
Where am I going wrong?
My whole code file is:
angular.module('ngMaterialTaskListApp')
.controller('MainCtrl', function ($scope, $mdDialog, TaskService) {
// Model from which View populates data
$scope.tasks = [];
console.log($scope.tasks);
$scope.showAddDialog = function (ev) {
$mdDialog.show({
controller: DialogController,
templateUrl: '../views/add-dialog-template.html',
parent: angular.element(document.body),
targetEvent: ev,
clickOutsideToClose: true,
fullscreen: true, //Only for xs and sm screen sizes
locals: { //For DialogController, as tasks
tasks: $scope.tasks
}
});
};
/*----------- Function to delete items onClick of delete icon -----------*/
$scope.delete = function (id) {
console.log($scope.tasks[id]);
console.log(id);
// console.log($scope.tasks[id].name);
$scope.tasks[id].deleted = true;
};
/*----------- DialogController function -----------*/
function DialogController($scope, $mdDialog, tasks) {
$scope.task = {};
$scope.hide = function () {
$mdDialog.hide();
//TODO Add a message as to what happened
};
$scope.cancel = function () {
$mdDialog.cancel();
//TODO Add a message as to what happened
};
/*----------- Method show the add dialog -----------*/
$scope.addData = function () {
if (null !== $scope.task.name && null !== $scope.task.description) {
/*----------- Using moment.js to parse date and time -----------*/
$scope.task.date = moment($scope.task.date, '').format('DD MMM YYYY');
$scope.task.time = moment($scope.task.time, '').format('h:mm a');
$scope.task.done = false; // Every new task is pending!
$scope.task.deleted = false; // Every new task exists!
var GlobalID = Date.now();
console.log(GlobalID);
$scope.task.id = GlobalID;
/*----------- Performing http POST -----------*/
TaskService.postTask($scope.task);
/*----------- Pushing to tasks object in $scope of MainCtrl -----------*/
// Have to update tasks again
tasks.push($scope.task);
$scope.hide();
console.log(tasks); //DEBUGGING
} else {
//TODO ADD INVALID/NULL DATA WARNING
}
};
};
// DEPRECATED - USED FOR DATA WHEN SERVER NOT AVAILABLE
TaskService.getTasks().then(function (response) {
$scope.tasks = response.data.tasks;
}, function (error) {
console.log(error + "This");
});
//USING THIS TO GET DATA FROM SERVER
TaskService.getAllTasks().then(function (response) {
// console.log(response.data);
$scope.tasks = response.data;
console.log($scope.tasks);
});
});
How is your html? I bet is like this inside a button in ng-repeat:
ng-click="delete(task.id)"
Try putting like this:
ng-click="delete($index)"
i have created the custom service like this
app.service('userService', function($http,UrlService) {
return {
init: function(callback) {
$http.get(UrlService.baseUrl +'/api/users/list').then(function(user_response) {
callback(user_response);
});
}
}
})
Inside of my project main controller i have used like this to get the angular material design modal.
$scope.replyComplaint = function(user,complaint_id) {
complaint_id=user._id;
console.log(complaint_id)
$mdDialog.show({
controller: DialogCtrl,
templateUrl: 'submodules/user_management/replydialog.html',
resolve: { complaint_id : function() {return complaint_id;} },
locals: {
users: $scope.users
},
parent: angular.element(document.body),
clickOutsideToClose: true,
})
.then(function(response) {
$scope.response = response;
console.log(response);
}, function() {
//fail
});
};
created another controller for dialog as in the angular material docs as follows
function DialogCtrl($scope, $rootScope, $mdDialog, users,complaintService, UrlService, $http) {
complaintService.init(function(complaint_response) {
$scope.complaints = complaint_response.data;
$scope.getUsers();
});
$scope.getUsers = function(complaint_id) {
console.log(complaint_id);
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.replyMail = function(complaint_id) {
console.log(complaint_id);
$http.post(UrlService.baseUrl + '/api/complaints/complaint/'+complaint_id , {
complaint: "replyText"
}, $scope)
.then(function(response) {
console.log(name);
$state.reload();
}, function(response) {
console.log(name);
});
}
}
}
Now, i need to get the user_response data in DialogController. if i put console.log('$scope.users') inside of this userservice.init function, i can get the data. but not outside of it. how to get the response data outside of the userService.init function
userService.init(function(user_response) {
$scope.users = user_response.data;
}); //this is added in DialogController
Main intension is to get the user.comlaint_id in the post request of reply mail function . that user.complaint_id is a part of the user_response
Anyone please help me. Thanks
The $http.get call returns a promise, you can just use that.
app.service('userService', function($http,UrlService) {
return {
init: function(callback) {
return $http.get(UrlService.baseUrl +'/api/users/list');
}
}
});
Controller:
function Dialog($scope,$rootScope, $mdDialog,userService,UrlService,$http) {
// console.log(userService.init());
init();
function init() {
userService.init().then(function(response) {
$scope.users = response.data;
});
}
}
This also has the advantage of easier error handling:
function Dialog($scope,$rootScope, $mdDialog,userService,UrlService,$http) {
// console.log(userService.init());
init();
function init() {
userService.init().then(function(response) {
$scope.users = response.data;
}, function(error) {
// handle error
});
}
}
You should read up on angular/javascript promises and their chaining mechanism: angular promises
Here is the solution
userService.init(function(user_response) {
$scope.users = user_response.data;
$scope.init();
});
$scope.init = function() {
You can access $scope.users here
}
Call any method instead of init() in which you require $scope.users
I have the following code snippet. I'm trying to clear the comment, but I can't do it from within the response. Could someone help me to figure out how to do this.
app.controller('CommentController', function($scope, $http) {
$scope.addReview = function(obj) {
this.comment.id = obj.id;
$http.post('/save', this.comment).
then(function(response) {
obj.comments.push(response.data);
this.comment = {};
}, function(response) {
});
};
});
it is because of the scope: 'this' is refering to the 'then' callback.
Try this:
app.controller('CommentController', function($scope, $http) {
$scope.addReview = function(obj) {
// retrieve the scope
var me = this;
this.comment.id = obj.id;
$http.post('/save', this.comment).
then(function(response) {
obj.comments.push(response.data);
// use the upper reference
me.comment = {};
}, function(response) {
});
};
});
Moreover, you would probably use the $scope.$apply function.
This is a follow-up question to Angular-ui modal, sending data into modal controller from $http
I have the following code where I want to get data via a factory to the modal.
$scope.docSetup = function() {
var modalInstance = $modal.open({
templateUrl : '/templates/dialog/docSetup.html',
controller : 'docSetupDlgCtrl',
resolve : {
dlgData : function(){
return TagService.list($scope.publication.id);
}
}
});
modalInstance.result.then(function (dlgData) {
$log.debug(dlgData);
}, function () {
$log.debug('Modal dismissed at: ' + new Date());
});
};
And here is the factory:
app.factory("TagService", function($http, $log){
return {
list: function(selectedDoc){
$log.info("Tag service at work => list");
var httpPromise = $http.post("tags/list", { publicationId: selectedDoc });
httpPromise.then(function (response) {
$log.log(response.data);
return response.data;
}, function (error) {
$log.error(error);
});
}
}
});
The above isn't resolving any data into dlgData. The factory is producing data and if I hardcode the data object into the 'resolve' function, it passes it.
return the entire httpPromise as well:
return httpPromise.then(function (response) {
$log.log(response.data);
return response.data;
}, function (error) {
$log.error(error);
});
Im just starting on AngularJS. I'm not sure how to churn this out. I'm trying to include multiple functions within one service. (I hope this is not against bad practice.)
The following is my working code:
myDataService.async().then(function (d) {
$scope.dbCalls = d.d;
});
My Service:
app.factory('myDataService', function ($http) {
// How do you get this bottom line to work?
// this.getAllCalls = function () {
var myService = {
async: function () {
var promise = $http.post('AngularTest.aspx/FetchCalls', { data: {} }).then(function (response) {
console.log(response);
return response.data;
});
return promise;
}
};
return myService;
//}; <--Commented out for clarity
});
Thanks!
you just return an object with properties from the service, then you are able to call those properties as different service methods
like so:
.service('myService', function() {
return {
firstMethod: function() { ... },
secondMethod: function() { ... },
thirdMethod: function() { ... }
}
})
and in the controller/directive
.controller('myCtrl', function(myService) {
myService.firstMethod();
myService.secondMethod();
myService.thirdMethod();
})