Curriculum
Course: AngularJS
Login

Curriculum

AngularJS

AngularJS Tutorial

0/65
Text lesson

Directives (Used Above) Explained

AngularJS Directive

Description

<body ng-app

Sets up an application for the <body> element.

<body ng-controller

Establishes a controller for the <body> element.

<tr ng-repeat

Repeats the <tr> element for each user in the users array.

<button ng-click

Call the editUser() function when the <button> element is clicked.

<h3 ng-show

Display the <h3> element if edit is true.

<h3 ng-hide

Hide the form if hideform is true, and hide the <h3> element if edit is true.

<input ng-model

Bind the <input> element to the application using data binding.

<button ng-disabled

Disables the <button> element if error or incomplete is true.

Code7.CSS Classes Explained

Element

Class

Defines

<div>

code7-container

A content container

<table>

code7-table

A table

<table>

code7-bordered

A bordered table

<table>

code7-striped

A striped table

<button>

code7-btn

A button

<button>

code7-green

A green button

<button>

code7-ripple

A ripple effect when you click the button

<input>

code7-input

An input field

<input>

code7-border

A border on the input field

JavaScript Code

myUsers.js

angular.module(‘myApp’, []).controller(‘userCtrl’function($scope) {
$scope.fName ;
$scope.lName = ;
$scope.passw1 = ;
$scope.passw2 = ;
$scope.users = [
{id:1, fName:‘Hege’, lName:“Pege” },
{id:2, fName:‘Kim’,  lName:“Pim” },
{id:3, fName:‘Sal’,  lName:“Smith” },
{id:4, fName:‘Jack’, lName:“Jones” },
{id:5, fName:‘John’, lName:“Doe” },
{id:6, fName:‘Peter’,lName:“Pan” }
];
$scope.edit = true;
$scope.error = false;
$scope.incomplete = false;
$scope.hideform = true;
$scope.editUser = function(id) {
  $scope.hideform = false;
  if (id == ‘new’) {
    $scope.edit = true;
    $scope.incomplete true;
    $scope.fName = ;
    $scope.lName ;
    } else {
    $scope.edit = false;
    $scope.fName = $scope.users[id-1].fName;
    $scope.lName = $scope.users[id-1].lName;
  }
};

$scope.$watch(‘passw1’,function() {$scope.test();});
$scope.$watch(‘passw2’,function() {$scope.test();});
$scope.$watch(‘fName’function() {$scope.test();});
$scope.$watch(‘lName’function() {$scope.test();});

$scope.test = function() {
  if ($scope.passw1 !== $scope.passw2) {
    $scope.error = true;
    } else {
    $scope.error = false;
  }
  $scope.incomplete = false;
  if ($scope.edit && (!$scope.fName.length ||
  !$scope.lName.length ||
  !$scope.passw1.length || !$scope.passw2.length)) {
     $scope.incomplete = true;
  }
};

});

JavaScript Code Explained

Scope Properties

Used for

$scope.fName

Model variable (user first name)

$scope.lName

Model variable (user last name)

$scope.passw1

Model variable (user password 1)

$scope.passw2

Model variable (user password 2)

$scope.users

Model variable (array of users)

$scope.edit

Set to true when user clicks on ‘Create user’.

$scope.hideform

Set to false when user clicks on ‘Edit’ or ‘Create user’.

$scope.error

Set to true if passw1 not equal passw2

$scope.incomplete

Set to true if any field is empty (length = 0)

$scope.editUser

Sets model variables

$scope.$watch

Watches model variables

$scope.test

Tests model variables for errors and incompleteness