Skip to content Skip to sidebar Skip to footer

How To Access A Specific $scope Variable When Loading A Different .html File?

First of all, sorry about the title, I just wan't sure how to word this question. So I'm making a task manager using AngularJS. I have a form for the user to fill with the details

Solution 1:

to access this variable in other html you can use factory or service like this

(function() {
"use strict";
angular.module('dataModule',[])
.factory('datafactory',function(){
return {

};
});
})();

Now datafactory is factory we need to inject this module(dataModule) in your module and factory(datafactory) in controller

Now how to use this factory in your function

$scope.details = function (task)
{
datafactory.currentTask =task
$window.location.href = 'table.html';
}

Now this datafactory stores your variable that can we used in any controller and later on also you can use this factory to store any such variable for global use like this datafactory.Myvariable ="hasd"//assign here

Now to use this variable Suppose you want to use this variable in another page table.html there on

// in html ng-init ="$scopeInit()"

in controller
$scopeInit =function(){
$scope.localTask =datafactory.currentTask
}
anduse $scope.localTask

suppose html looks something like this

<div ng-controller ="my-controller" ng-init ="$scopeInit()">
<tableid="datatable"class="display"ng-cloak><thead><tr><th><b>ID</b></th><th><b>Title</b></th><th><b>Start Date</b></th><th><b>End Date</b></th><th><b>Type</b></th><th></th></tr></thead><tbody><trng-repeat="task in tasks track by $index"><tdng-click="details(task)">{{task.id}}</td><tdng-click="details(task)">{{task.title}}</td><tdng-click="details(task)">{{task.start_date}}</td><tdng-click="details(task)">{{task.end_date}}</td><tdng-click="details(task)">{{task.type}}</td><td><ang-click="remove(task)"class="btn-floating waves-effect waves-light red<i class="material-icons">clear</i></a></td></tr></tbody></table>

<div>

//in controller

$scopeInit =function(){
$scope.task =datafactory.currentTask
}

//$scope.task contains required array and hence table can be created

Post a Comment for "How To Access A Specific $scope Variable When Loading A Different .html File?"