Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

AngularJS Bootstrap


May 08, 2021 AngularJS


Table of contents


AngularJS Bootstrap

This section describes how to work in your Bootstrap is used in AngularJS.

Bootstrap contains a number of Web components that allow you to quickly build a beautiful, fully functional Web site.


AngularJS's preferred style sheet is Twitter Bootstrap, which is currently the most popular front-end framework.

Check out bootstrap tutorials.


Bootstrap

You can add Twitter Bootstrap to your AngularJS app, and you can add the following code to your element:

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="external nofollow" target="_blank" >

If the site is in China, it is recommended to use Bootstrap from Baidu's static repository, the code is as follows:

<link rel="stylesheet" href="//apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css" rel="external nofollow" target="_blank" >

The following is a complete HTML instance, using the AngularJS directive and bootstrap classes.


HTML code

<! DOCTYPE html>
<html ang-app="">
<head>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css" rel="external nofollow" target="_blank" >
</head>

<body ng-controller="userController">
<div class="container">

<h3>Users</h3>

<table class="table table-striped">
<thead><tr>
<th>Edit</th>
<th>First Name</th>
<th>Last Name</th>
</tr></thead>
<tbody><tr ng-repeat="user in users">
<td>
<button class="btn" ng-click="editUser(user.id)">
<span class="glyphicon glyphicon-pencil"></span> &nbsp; Edit
</button>
</td>
<td>{{ user.fName }}</td>
<td>{{ user.lName }}</td>
</tr></tbody>
</table>

<hr>
<button class="btn btn-success" ng-click="editUser('new')">
<span class="glyphicon glyphicon-user"></span> Create New User
</button>
<hr>

<h3 ng-show="edit">Create New User:</h3>
<h3 ng-hide="edit">Edit User:</h3>

<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">First Name:</label>
<div class="col-sm-10">
<input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Last Name:</label>
<div class="col-sm-10">
<input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Password:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw1" placeholder="Password">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Repeat:</label>
<div class="col-sm-10">
<input type="password" ng-model="passw2" placeholder="Repeat Password">
</div>
</div>
</form>

<hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
<span class="glyphicon glyphicon-save"></span> Save Changes
</button>
</div>

<script src = "http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
<script src = "myUsers.js"></script>
</body>
</html>>

Try it out . . .


Instruction resolution

AngularJS instructions Describe
<html ng-app Define an app for the element (unnamed)
<body ng-controller Define a controller for the element
<tr ng-repeat Loops an array of user objects, each of which is placed in the element of the .lt;tr?gt;
<button ng-click Call the function editUser() when clicking on the element
<h3 ng-show If edit s true displays the elements of the .lt;h3>
<h3 ng-hide If edit s true hides the elements of the .lt;h3>
<input ng-model Bind the element for the application
<button ng-disabled If an error occurs, or if the ncomplete is true, disable the element


Bootstrap class resolution

Elements Bootstrap class Defined
<div> container The content container
<table> table Form
<table> table-striped A table with a striped background
<button> btn Button
<button> btn-success The success button
<span> glyphicon Glyph icon
<span> glyphicon-pencil Pencil icon
<span> glyphicon-user User icon
<span> glyphicon-save Save icon
<form> form-horizontal Horizontal form
<div> form-group Form group
<label> control-label Controller tag
<label> col-sm-2 Cross 2 columns
<div> col-sm-10 Cross 10 columns


JavaScript code

function userController($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.editUser = function(id) {
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 parsing

Scope property Use
$scope.fName Model variable (user name)
$scope.lName Model variable (user's last name)
$scope.passw1 Model variables (user password 1)
$scope.passw2 Model variables (user password 2)
$scope.users Model variables (array of users)
$scope.edit Set to true when the user clicks Create user.
$scope.error If passw1 is not equal to passw2 is set to true
$scope.incomplete If each field is empty (length s 0) is set to true
$scope.editUser Set the model variable
$scope.watch Monitor model variables
$scope.test Verify the error and integrity of model variables