angularjs - Can't access properties of my Javascript object -
i'm using angular.js fetch single record api. i'm getting record object, can log object , see it's properties cannot access of properties. undefined
.
var template = template.get({id: id}); $scope.template = template; ... console.log(template); // displays object console.log(template.content); // undefined
update
var id = $routeparams.templateid; var template = template.get({id: id}); $scope.template = template; /*** template placeholders ***/ $scope.updateplaceholders = function () { var placeholders = []; var content = template.content; console.log(template); // dumps object in screenshot console.log("content" in template); // displays false // placeholders match patter var match = content.match(/{([a-z0-9]+)}/gmi); ... } $scope.$on('$viewcontentloaded', function(){ $scope.updateplaceholders(); });
you need wait http request complete, specify in callback. in case i've taken step further , added listener template object there's no callback dependency between updateplaceholders , resource.
var id = $routeparams.templateid; var template = template.get({id: id}, function(res) { $scope.template = template; }); /*** template placeholders ***/ $scope.updateplaceholders = function () { var placeholders = []; var content = $scope.template.content; console.log($scope.template); console.log("content" in $scope.template); // placeholders match patter var match = content.match(/{([a-z0-9]+)}/gmi); ... } $scope.$watch('template', function(newvalue){ if(newvalue) $scope.updateplaceholders(); });
Comments
Post a Comment