SPTutorialsList
, and add a controller named SPController
to it.
Controller adds an array named tutorials
to the current $scope
. The directive ng-repeat
is used to display a list using the items in the array.
index.html
[html]
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.3.14" data-semver="1.3.14" src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="Controller.js"></script>
</head>
<body>
<div ng-app="SPTutorialsList" ng-controller="SPController">
<ul>
<li ng-repeat="x in products">{{x}}</li>
</ul>
</div>
<p>So far we have made an HTML list based on the tutorials of an array.</p>
</body>
</html>
[/html]
Controller.js
[javascript]
var app = angular.module("SPTutorialsList", []);
app.controller("SPController", function($scope) {
$scope.products = ["AngularJS", "Bootstrap", "JavaScript"];
});
[/javascript]
Output:
ng-model
directive by adding a text-field.
In the controller, create a function with name addTutorial
, and use addsp
input field value to add a tutorial to the tutorials array. Add a button, and give it an ng-click
directive that will run the addTutorial
function when the button is clicked.
index.html
[html]
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.3.14" data-semver="1.3.14" src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="Controller.js"></script>
</head>
<body>
<div ng-app="SPTutorialsList" ng-controller="SPController">
<ul>
<li ng-repeat="x in tutorials">{{x}}</li>
</ul>
<input ng-model="addsp" />
<button ng-click="addTutorial()">Add</button>
</div>
<p>Write in the input field to add Tutorials.</p>
</body>
</html>
[/html]
Controller.js
[javascript]
var app = angular.module("SPTutorialsList", []);
app.controller("SPController", function($scope) {
$scope.tutorials = ["AngularJS", "Bootstrap", "JavaScript"];
$scope.addTutorial = function () {
$scope.tutorials.push($scope.addsp);
}
});
[/javascript]
Output:
removeTutorial
, which considers it as a parameter as the index of the tutorial is to be removed.
In the HTML, make a element for each tutorial, and give them an ng-click
directive which calls the removeTutorial function with the current $index
.
index.html
[html]
<!DOCTYPE html>
<html>
<head>
<script data-require="angular.js@1.3.14" data-semver="1.3.14" src="https://code.angularjs.org/1.3.14/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="Controller.js"></script>
</head>
<body>
<script>
</script>
<div ng-app="SPTutorialsList" ng-controller="SPController">
<ul>
<li ng-repeat="x in tutorials">{{x}}
<span ng-click="removeTutorial($index)" ><span style="color: #f00;">x</span></span>
</li>
</ul>
<input ng-model="addsp" />
<button ng-click="addTutorial()">Add</button>
</div>
<p>Click the little x to remove a tutorial from the Tutorials list.</p>
</body>
</html>
[/html]
Controller.js
[javascript]
var app = angular.module("SPTutorialsList", []);
app.controller("SPController", function($scope) {
$scope.tutorials = ["AngularJS", "Bootstrap", "JavaScript"];
$scope.addTutorial = function () {
$scope.tutorials.push($scope.addsp);
}
$scope.removeTutorial = function (x) {
$scope.tutorials.splice(x, 1);
}
});
[/javascript]
Output: