1

I'm new to .component() in Angular 1.5+ and I'm trying to create data in the top level app controller, and pass it to a child component. However, printing to the console gives me undefined in the following example and I'm not sure why.

index.html

<!DOCTYPE html>
<html>
<head>
    <script src="Scripts/angular.js"></script>
    <script src="app.js"></script>
    <title></title>
    <meta charset="utf-8" />
</head>
<body ng-app="myApp" ng-controller="myController as vm">
    <app name="vm.myName"></app>
</body>
</html>

app.js

var app = angular.module("myApp", []);

app.controller("myController", ['$scope', function ($scope) {
    console.log("myController!");

   $scope.myName = "Rick";
}]);

app.component("app", {
    template: '<h1>Hello World</h1>',
    bindings: {
        name: '='
    },
    controller: function () {
        console.log(this.name); <-- prints undefined
    }
});

3 Answers 3

1

The problem is that in the controller you are using the $scope to define the variable that is getting passed into the component but, in the controller block of the HTML you are using the this syntax for passing the variable into the component.

So the line with the problem is.

app.controller("myController", ['$scope', function ($scope) {
    console.log("myController!");
   $scope.myName = "Rick";
}]);

This is a standard scope syntax, but since we are using this syntax we need it to be.

app.controller("myController", ['$scope', function ($scope) {
    console.log("myController!");
    this.myName = "Rick";
}]);

Below is a working example.

JSFiddle Demo

Sign up to request clarification or add additional context in comments.

Comments

0

It is because you are trying to access it too soon. There is a component lifecycle hook called $onInit. All bindings will be resolved in that function, so you can use that to access your name property.

Try to modify your controller to something like this:

controller: () => {

    this.$onInit = () => {
        console.log(this.name);
    }

}

2 Comments

That makes sense, but still getting undefined.
It works fine when I pass from one component to another, but when trying to pass that top level controller variable to a component it's giving undefined.
0

$onChanges runs before $onInit. You can simply check when your value is passed to your component using this block of code:

this.$onChanges = (changesObj) => {
    if(changesObj.name && changesObj.name.currentValue) {
        console.log(this.name);
    }
}

You might want to take further look at angularjs 1.5+ components' documentation

1 Comment

It's not hitting this console.log() but that is the question. Why isn't the ng-controller variable being passed to the component. I mean I knew it wasn't which is why it was giving me undefined, but why and how do you get it to pass.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.