4

Here is my html and angular js code

<html>
<head>
    <title></title>
    <script src="Scripts/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript" language="javascript">
        function DemoController($scope) {
            $scope.user = {
                dateOfBirth: new Date(1970, 0, 1)
            }
        }
    </script>
</head>
<body>
    <div ng-app="demo" ng-controller="DemoController">
        Date Of Birth:
        <my-datepicker type="text" ng-model="user.dateOfBirth" />
        <br>
        Current user's date of birth: <span id="dateOfBirthDisplay">{{user.dateOfBirth}}</span>
    </div>
</body>
</html>

It doesn't work and gives output

    Date Of Birth: 
    Current user's date of birth: {{user.dateOfBirth}}

But if i remove ="demo" from ng-app="demo" in the div it works...

3 Answers 3

7

When you assign a name to the ng-app directive, you essentially create a module with that name. In this module, you will then have to define your directives, services, filters and configurations.

In your case, when you assigned the name "demo", you created a module named "demo". Function DemoController now is no longer part of this module.

To use this function AND assign a module to your application, you need to use the following way of defining a controller:

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

app.controller('DemoController', [function () {
    //The body of demo controller
}]);

This way, the application knows which module's controller needs to be associated for the scope of the corresponding view.

EDIT: The ng-app directive reference

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

Comments

1

My solution works, try this : http://plnkr.co/edit/zwOKitT8wznO17yZfz15?p=preview

<body ng-app="">
    <div ng-controller="DemoController">
        Date Of Birth:
        <br>
        Current user's date of birth: <span id="dateOfBirthDisplay"><span ng-bind="user.dateOfBirth"></span></span>
    </div>
</body>

1 Comment

If there is no value to the ng-app directive, you can simply use ng-app without the ng-app="" code...
0

The value of ng-app attribute defines the application's module name. In your case that's "demo". While you haven't declared DemoController within the module "demo", using an angular.module call, it isn't visible within your ng-app html, executed in the scope of "demo" module.

What will work for ng-app="demo" is:

<script type="text/javascript" language="javascript">
    angular.module('demo', []).controller('DemoController', function ($scope) {
        $scope.user = {
            dateOfBirth: new Date(1970, 0, 1)
        }
    });
</script>

Comments

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.