Okay, from the top! :-)
First, you have to tell AngularJS somewhere that you're using it. The body
tag is as good a place as any: <body ng-app="myApp">
. This tells AngularJS to use a module called myApp
as the root of your application. So let's define that now:
// app.js
var app = angular.module( 'myApp', [] );
The second argument is an array of dependencies. We won't worry about that now. So now we have a module. AngularJS is an MVC framework, so we also need to define a controller somewhere. Let's add this to our app.js
file:
app.controller( 'MainCtrl', function( $scope ) {
// we control our app from here
});
The $scope
variable is how we glue our controller logic to the view. Speaking of the view, we have to tell AngularJS to use this controller someplace. Let's edit the body
tag once more:
<body ng-app="myApp" ng-controller="MainCtrl">
Boom! Our app works. So now AngularJS is all set up and working, so we can tackle your question.
You want to make something appear on a certain action. There's a directive for that called ngShow
. It will show whatever's inside of it when the condition is true:
<form ng-show="visible">
What is visible
? That's an expression. In this case, it's just a variable. Defined where? On our scope!
app.controller( 'MainCtrl', function( $scope ) {
$scope.visible = false;
});
Okay, so now it starts out as false
; how do we change it to true
when we click a button? There's a directive for that called ngClick
that also takes an expression:
<a class="btn" ng-click="visible = true">Show the Form</a>
In this case, our expression changes the visible
variable to true
. And immediately, the form is shown! Here's a completed Plunker: http://plnkr.co/edit/Kt4dR2tiTkShVYWCqQle?p=preview
Welcome to AngularJS!
Here are some supplemental resources you should grok:
And also join us on the Mailing List! We're good people.