AngularJS greetController Controller not working

Mr.Web

I'm on my way in learning AngularJS, I'm following a tutorial and I found myself stuck in a point as I cannot make the greetController controller to work.

Here the HTML:

<!DOCTYPE html>
<html>
<head>
    <title>HTML.it</title>
    <script type="text/javascript" src="angular.min.js"></script>
    <script type="text/javascript" src="app.js"></script>
</head>
<body>
    <div ng-app="myApp">
        <div ng-controller="userController">
            <p>Name: <input type="text" ng-model="user.fname"></p>
            <p>Lastname: <input type="text" ng-model="user.lname"></p>
        </div>
        <div ng-controller="greetController">
            <h3>{{greet()}}</h3>
        </div>
    </div>
</body>
</html>

Here the app.js:

(function(){
    var app = angular.module('myApp', []);

    app.controller('userController', function($scope){
        $scope.user = {fname: "Joe", lname: "Black"};
    });

    app.controller('greetController', function($scope){
        $scope.greet = function () {
            return "Hi " + $scope.user.fname + ' ' + $scope.user.lname; 
        };
    });

})();

The result is "{{greet()}}" with no output.

The console says: Cannot read property 'fname' of undefined

I guess I have the user.fname not readable from the greetController, why is that?

Sajeetharan

$scope.user user belongs to the controller named 'userController'

Hence it is undefined in the 'greetController'

You may choose $rootScope to Declare the user as a global object or consider using Service to share variable among controller.

One simple solution would be using the same userController.

Using Single Controller Plunker

Using $rootScope Plunker

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AngularJS greetController Controller not working

From Dev

AngularJS Controller not working in nested Controller

From Dev

angularjs 1.3 controller as not working

From Dev

multiple controller not working in angularjs

From Dev

AngularJS controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

AngularJS - Toaster not working in Controller

From Dev

Angularjs - data binding not working with controller

From Dev

AngularJS ng-controller not working

From Dev

Angularjs $setPristine not working with controller as syntax

From Dev

Show and Hide Controller not working, AngularJS

From Dev

Simple AngularJS Controller Demo Not Working

From Dev

AngularJS ng-click not working with configured Controller

From Dev

AngularJS beginner: ng-controller not working

From Dev

AngularJS ng-controller not working after bootstraping

From Dev

JS function defined in AngularJS controller not working

From Dev

AngularJS stops working as soon as I include a controller

From Dev

Angularjs controller not working, throws error (newbie)

From Dev

Cannot make controller tests working with AngularJS + RequireJS

From Dev

AngularJS controller with application don't working

From Dev

AngularJS controller.. Why the code id not working?

From Dev

AngularJS function in controller and factory not working when separated

From Dev

AngularJS Controller Issues - Click function stops working on page change

From Dev

passing parameters to resource factory in angularjs from the controller not working

From Dev

Inject AngularJS 1.2 $location service into controller not working in PhoneGap 3.0

From Dev

AngularJs ng-if function call from same controller not working

From Dev

AngularJS 1.3.8 Using multiple controllers, second controller is not working

From Dev

Set values to html tag from angularjs controller not working

From Dev

jquery metismenu not working loading data async with AngularJS controller

Related Related

  1. 1

    AngularJS greetController Controller not working

  2. 2

    AngularJS Controller not working in nested Controller

  3. 3

    angularjs 1.3 controller as not working

  4. 4

    multiple controller not working in angularjs

  5. 5

    AngularJS controller not working

  6. 6

    angularjs 1.3 controller as not working

  7. 7

    AngularJS - Toaster not working in Controller

  8. 8

    Angularjs - data binding not working with controller

  9. 9

    AngularJS ng-controller not working

  10. 10

    Angularjs $setPristine not working with controller as syntax

  11. 11

    Show and Hide Controller not working, AngularJS

  12. 12

    Simple AngularJS Controller Demo Not Working

  13. 13

    AngularJS ng-click not working with configured Controller

  14. 14

    AngularJS beginner: ng-controller not working

  15. 15

    AngularJS ng-controller not working after bootstraping

  16. 16

    JS function defined in AngularJS controller not working

  17. 17

    AngularJS stops working as soon as I include a controller

  18. 18

    Angularjs controller not working, throws error (newbie)

  19. 19

    Cannot make controller tests working with AngularJS + RequireJS

  20. 20

    AngularJS controller with application don't working

  21. 21

    AngularJS controller.. Why the code id not working?

  22. 22

    AngularJS function in controller and factory not working when separated

  23. 23

    AngularJS Controller Issues - Click function stops working on page change

  24. 24

    passing parameters to resource factory in angularjs from the controller not working

  25. 25

    Inject AngularJS 1.2 $location service into controller not working in PhoneGap 3.0

  26. 26

    AngularJs ng-if function call from same controller not working

  27. 27

    AngularJS 1.3.8 Using multiple controllers, second controller is not working

  28. 28

    Set values to html tag from angularjs controller not working

  29. 29

    jquery metismenu not working loading data async with AngularJS controller

HotTag

Archive