Reputation: 1423
I know it seems like a duplicate but it isn't. Nothing worked, whatever I tried.
I have a list in my angular module:
this.checkedInterviews = []
and then a function that does:
var interviewModel = {
interviewId: self.pendingInterviews[i].id,
status: self.pendingInterviews[i].status,
location: self.pendingInterviews[i].location,
start: self.pendingInterviews[i].start,
hideCheck: null
};
this.checkedInterviews.push(JSON.stringify(interviewModel));
I get Cannot read property 'push' of undefined
.
ANy idea what the problem is?
Upvotes: 2
Views: 1818
Reputation: 944
Try this Here is working fiddle
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.checkedInterviews = [];
$scope.pendingInterviews = [{'id':1,'status':'pending','location':'abc','start':'start'}];
for(i= 0; i < $scope.pendingInterviews.length; i++){
var interviewModel = {
interviewId: $scope.pendingInterviews[i].id,
status: $scope.pendingInterviews[i].status,
location: $scope.pendingInterviews[i].location,
start: $scope.pendingInterviews[i].start,
hideCheck: null
};
console.log(interviewModel);
$scope.checkedInterviews.push(JSON.stringify(interviewModel));
}
console.log($scope.checkedInterviews);
}
Upvotes: 0
Reputation: 8358
It seems, second function is using different this
.
In Angular Module, you can assign this
to some variable, then try to access it from second function.
E.g.:
var vm = this;
vm.checkedInterviews = [];
Now in function you should access it using:
vm.checkedInterviews.push();
Upvotes: 1
Reputation: 21852
var checkedIntervews = []
var interviewModel = {};
checkedIntervews.push(JSON.stringify(interviewModel));
console.log(checkedIntervews);
(function arrayPush() {
this.checkedIntervews = [];
var interviewModel = {};
this.checkedIntervews.push(JSON.stringify(interviewModel));
console.log(this.checkedIntervews);
})();
You want to try:
var checkedIntervews = []
var interviewModel = {};
checkedInterviews.push(JSON.stringify(interviewModel));
$scope.checkedInterviews = checkedInterviews; //If using AngularJS because Angular is tagged in the question
NOTE: You should be able to use this
if all of this is in the same function. This should work in the global scope as well. The only reason why I've used a IIFE is to separate out the scopes
Snippet added above with both cases.
If is not clear what this
is in your question btw.
Upvotes: 3
Reputation: 3324
If they are putting in different functions then this
in the second function is a different object.
Upvotes: 1