jetcom jetcom - 10 months ago 94
AngularJS Question

Why is the variable set in ng-init undefined in $scope in AngularJS?

I have a variable I'm setting in my view with

which I am then trying to access in my controller. When I try to access it however, I'm getting an

Relevant pieces of code:

My view:

<div ng-controller="MyCtrl">
<div ng-init="pageTitle = 'kittens'">

My controller:

var MyCtrl;

MyCtrl = function($scope) {
return console.log($scope.pageTitle);


What am I missing here?

Answer Source

Wait until the variable is init.

$scope.$watch('pageTitle', function () {