To store data locally in your browser, you have the option of using localstorage or sessionstorage. You can check out the differences between them here.
This code snippet illustrates how to implement this for your specific scenario:
angular.module('myApp', [])
.controller('myCtrl', ['$scope', function($scope) {
var saveVideosToStorage = function(){
localStorage.setItem('videos', angular.toJson($scope.videos));
}
var init = function() {
// Retrieve video array from localstorage/sessionStorage
var videos = angular.fromJson(localStorage.getItem('videos'));
if (!(videos instanceof Array)) {
// Video data has been corrupted
$scope.videos = [];
saveVideosToStorage();
}
$scope.videos = videos;
}
var addVideoUrl = function(text) {
$scope.videos.push(angular.copy(text)); // Save new data into 'videos' array
saveVideosToStorage();
}
$scope.addVideoUrl = addVideoUrl;
init();
}]);
Here is the corresponding markup:
<div ng-repeat="video in videos track by $index">{{video}}</div>
<input ng-model="videoUrl" type="text"/>
<input type="button" ng-click="addVideoUrl(videoUrl);">
You can view the working example on Plunker.
NOTE:
In this code snippet, I used $scope
instead of declaring a variable like var vm = this
.
If you need a more straightforward explanation about localStorage, sessionStorage, and cookies, check it out here.