首先,对标题感到抱歉,我只是不确定如何说出这个问题。
所以我正在使用AngularJS创建一个任务管理器。我有一个表单供用户在创建新任务时填写详细信息。我使用ng-model
将这些值保存到$scope
。以下是我如何保存它们:
$scope.add = function(tasks)
{
{
$scope.tasks.push({
'id': tasks.id,
'title': tasks.title,
'start_day': tasks.start_day,
'start_time':tasks.start_time,
'start_date':tasks.start_day + " " + tasks.start_time,
'end_day': tasks.end_day,
'end_time': tasks.end_time,
'end_date':tasks.end_day + " " + tasks.end_time,
'type': tasks.type,
'description': tasks.description
});
localStorage.setItem('tasks',JSON.stringify($scope.tasks));
}
};
然后,如您所见,我将这些值保存到本地存储。我有一个名为tasks
的数组,其中包含用户创建的所有任务。然后我在表格中显示这些任务:
<table id="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>
<tr ng-repeat="task in tasks track by $index">
<td ng-click="details(task)">{{task.id}}</td>
<td ng-click="details(task)">{{task.title}}</td>
<td ng-click="details(task)">{{task.start_date}}</td>
<td ng-click="details(task)">{{task.end_date}}</td>
<td ng-click="details(task)">{{task.type}}</td>
<td><a ng-click="remove(task)" class="btn-floating waves-effect waves-light red<i class="material-icons">clear</i></a></td>
</tr>
</tbody>
</table>
然后,目标是您可以单击任何行并加载包含所有任务详细信息的新页面。我使用函数details()
执行此操作,该函数具有以下代码:
$scope.details = function (task)
{
$window.location.href = 'table.html';
}
这会加载文件table.html
,我想要的是在此页面中显示所有任务详情,即ID,标题,描述等。
我不知道的是如何仅显示您点击的特定任务。例如,如果我点击任务“Todo#1”的行,我只想查看任务“Todo#1”的详细信息。
答案 0 :(得分:2)
要在其他html中访问此变量,您可以使用像这样的工厂或服务
(function() {
"use strict";
angular.module('dataModule',[])
.factory('datafactory',function(){
return {
};
});
})();
现在数据是工厂我们需要在你的模块和工厂(datafactory)中注入这个模块(dataModule)
现在如何使用这个工厂 在你的功能
$scope.details = function (task)
{
datafactory.currentTask =task
$window.location.href = 'table.html';
}
现在这个datafactory存储了我们可以在任何控制器中使用的变量,以后你也可以使用这个工厂存储任何这样的变量供全局使用 喜欢这个datafactory.Myvariable =&#34; hasd&#34; //在这里分配
现在使用这个变量 假设你想在另一个页面table.html中使用这个变量
// in html ng-init ="$scopeInit()"
in controller
$scopeInit =function(){
$scope.localTask =datafactory.currentTask
}
and use $scope.localTask
假设html看起来像这样
<div ng-controller ="my-controller" ng-init ="$scopeInit()">
<table id="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>
<tr ng-repeat="task in tasks track by $index">
<td ng-click="details(task)">{{task.id}}</td>
<td ng-click="details(task)">{{task.title}}</td>
<td ng-click="details(task)">{{task.start_date}}</td>
<td ng-click="details(task)">{{task.end_date}}</td>
<td ng-click="details(task)">{{task.type}}</td>
<td><a ng-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