0

I'm just starting out with AngularJS and have hit a really simple snag that I just can't seem to get past. All I want to do is initialize some values when the page loads. It doesn't happen. Here's the code:

var bookingAppModule = angular.module('bookingApp', []);

bookingAppModule.controller('bookingController', ['$scope', function ($scope) {
    $scope.bookingDate = '2014/04/28';
    $scope.alertMessage = "initialized";
}]);


</script>
<div id ="booking-app" ng-app="bookingApp"">
<div >
    {{bookingDate}}<br />
    <input type="date" ng-model="bookingDate" />
    <br />
    {{alertMessage}}
</div>
</div>

What am I missing here? BTW, I also tried putting an init() function on the controller and using ng-init= "init()", but that doesn't work either. I can initialize the values using ng-init ="bookingDate= ...", but that is not what I want.

1
  • Just stick ng-controller="bookingController" in one of those first divs. Commented Apr 29, 2014 at 0:56

1 Answer 1

3

Put the ng-app attribute in the HTML tag. Reference ng-controller in the booking-app div like this:

<div id ="booking-app" ng-controller="bookingController"">
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I thought the line bookingAppModule.controller(... had the effect of assigning the controller. Can you explain?
That line just creates the controller. Angular can handle multiple views and controllers so you have to define the context in which the controller is being used. Once you start using routing this will become clearer. docs.angularjs.org/guide/controller#!

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.