Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Getting Started with Visual Studio Code and AngularJS

Tech 2

Visual Studio Code Setup

Download the IDE from: https://code.visualstudio.com/ — choose the stable build.

Applying Chinese Language Pack:

Open the Extensions panel on the left sidebar, search for "chinese", install the extension, then restart the editor.

Editor Layout Overview

The VS Code interface consists of five main regions:

  1. Activity Bar — Contains shortcuts for opening the sidebar, search, Git integration, debugger, and extensions.
  2. Sidebar — Manage project files and folders.
  3. Editor Area — Write and edit code.
  4. Panel Bar — Includes Problems, Output, Debug Console, and Terminal tabs. The terminal is essential for running CLI commands.
  5. Status Bar — Displays file and project information.

If the panel bar is hidden, enable it via: View → Appearance → Show Panel.

Git Initialization

When attempting to commit changes manual via Git, you might encounter:

fatal: not a git repository (or any of the parent directories): .git

This occurs because no local repository exists yet. Initialize one with:

git init

Running HTML Files in Browser

Install the "open in browser" extension from the marketplace. Right-click any HTML file and select "Open in Default Browser" to preview.


AngularJS Fundamentals

AngularJS is a JavaScript framework distributed as a single library file. Include it via a script tag placed at the bottom of the <body> for optimal page load performance:

<script src="https://cdn.staticfile.org/angular.js/1.4.6/angular.min.js"></script>

Understanding Scope

An AngularJS application consists of three parts:

  • View — The HTML DOM
  • Model — Available data within the current view
  • Controller — JavaScript functions that manage data and logic

The $scope object serves as the model and bridges data between the controller and view. Properties and methods attached to $scope become accessible in the view without the prefix—simply referance them directly like {{propertyName}}.

Root Scope

Every Angular application has a $rootScope that operates across the entire application wherever ng-app is defined. It acts as a parent scope enabling data sharing across different controllers.

<div ng-app="myApp" ng-controller="mainCtrl">
<h1>{{surname}} Family:</h1>
<ul>
    <li ng-repeat="member in members">{{member}} {{surname}}</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('mainCtrl', function($scope, $rootScope) {
    $scope.members = ["Alice", "Bob", "Charlie"];
    $rootScope.surname = "Smith";
});
</script>

Core Directives

Directive Purpose
ng-app Marks the root element of an AngularJS application
ng-controller Attaches a controller to the view
ng-model Binds input values to application variables
ng-bind Binds element content to application data
ng-repeat Iterates over collections

HTML5 supports custom attributes prefixed with data-. Use data-ng-* as a valid HTML5 alternative to ng-*.

Controller Patterns

Controller as Property:

<div ng-app="" ng-controller="userController">
First Name: <input type="text" ng-model="user.firstName"><br>
Last Name: <input type="text" ng-model="user.lastName"><br>
</div>
<script>
function userController($scope) {
    $scope.user = {
        firstName: "Jane",
        lastName: "Doe"
    };
}
</script>
<script src="https://cdn.staticfile.org/angular.js/1.2.5/angular.min.js"></script>

Key distinctions:

  • Requires the AngularJS libray script tag
  • Cannot use the ng-app attribute value when using global controller functions
  • Object properties use colon notation with comma separators

Controller as Method:

<div ng-app="myApp" ng-controller="personCtrl">
First Name: <input type="text" ng-model="firstName"><br>
Last Name: <input type="text" ng-model="lastName"><br>
Full Name: {{getFullName()}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.getFullName = function() {
        return $scope.firstName + " " + $scope.lastName;
    };
});
</script>

Two-Way Data Binding and Form Validation

<div ng-app="app">
    <div ng-controller="mainCtrl">
        Name: <input ng-model="name">
        <h1>Hello {{name}}</h1>
        <h2 ng-bind="name"></h2>
    </div>
    <form name="userForm">
        Email: <input type="email" name="emailInput" ng-model="email"><br>
        <span ng-show="userForm.emailInput.$error.required">Email is required.</span>
        <span ng-show="userForm.emailInput.$error.email">Invalid email format.</span>
        <h1>Form State:</h1>
        Valid: {{userForm.emailInput.$valid}}<br>
        Invalid: {{userForm.emailInput.$invalid}}<br>
        Dirty: {{userForm.emailInput.$dirty}}<br>
        Pristine: {{userForm.emailInput.$pristine}}<br>
        Touched: {{userForm.emailInput.$touched}}<br>
        Untouched: {{userForm.emailInput.$untouched}}
    </form>
</div>
<style>
input.ng-invalid {
    background-color: lightblue;
}
</style>
<script>
var app = angular.module("app", []);
app.controller("mainCtrl", function($scope) {
    $scope.name = "Initial Value";
});
</script>

Form Validation Properties

Property Description
$valid Returns true when input passes validation
$invalid Returns true when input fails validation
$dirty Returns true when user has modified the input
$pristine Returns true when user has not modified the input
$touched Returns true when input has been focused and then blurred
$untouched Returns true when input has not been focused or blurred

Reference: https://www.runoob.com/angularjs/angularjs-intro.html

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.