使用角度传递的数据更新模态
Updating modal with data passing in angular
对
角度真的很陌生,但我有这个待办事项列表。我添加了一个模式按钮,我希望任务项在创建的每个模式中更新。由于某种原因,我无法获得模式来显示分配给它的任务项的正确名称。它卡在所有模态的引用索引 0 上。
第一个模态校正
第二模态仍然反映第一
这是 HTML:
<header>
<ul class="nav nav-tabs">
<li role="presentation" class="active">
<a href="#/">Home</a>
</li>
<li role="presentation">
<a href="#/second">History</a>
</li>
</ul>
</header>
<center>
<div>
<div class="form-group">
<div class="col-xs-12 col-sm-8 col-md-4 col-lg-4 col-centered" id="nv">
<div class="row"></div>
<input ng-model="newItem" ng-keypress="addItem($event)" type="text" class="form-control lead" id="nv" placeholder="Add a grocery.">
</div>
</div>
</div>
</center>
<div id="mainBox">
<ul class="list-group checked-list-box" id="repeatBox">
<li class="list-group-item col-xs-12 col-sm-8 col-md-4 col-lg-4 col-centered" id="liBox" ng-repeat="x in displayHold" >
<input type="checkbox" ng-model="deleted"></input>
<h7 id="lItem" class="lead" ng-class="{strike: deleted,notActive: deleted}">{{x.item}}</h7>
<button type="button" class="btn btn-default pull-right" ng-click="rmList(x)">
<span class="glyphicon glyphicon-trash" id="icon"></span>
</button>
<button type="button" class="btn btn-default pull-right" data-toggle="modal" data-target="#myModal">
<span class="glyphicon glyphicon-plus" id="icon"></span>
</button>
<div class="container">
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="btn btn-default" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{modalItem(x)}}</h4>
</div>
<div class="modal-body lead">
<p>{{displayHold.x}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
和 js:
var app = angular.module('app', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'templates/home.html',
controller: 'homeCTRL'
})
.when('/second', {
templateUrl: 'templates/second.html',
controller: 'secondCTRL'
})
.otherwise({
redirectTo: '/'
});
}]);
//services
//history
app.service('carry', function() {
var transferAr = [];
var addList = function(newObj) {
transferAr.push({
item: newObj
});
};
var getList = function() {
return transferAr;
};
return {
addList: addList,
getList: getList,
};
});
//home temporary
app.service('hold', function() {
var holdTransferAr = [];
var holdAddList = function(newObj) {
holdTransferAr.push({
item: newObj
});
};
var holdGetList = function() {
return holdTransferAr;
};
return {
holdAddList: holdAddList,
holdGetList: holdGetList,
};
});
//controllers
app.controller('homeCTRL', ['$scope', 'carry', 'hold', '$log', function($scope, carry, hold, $log) {
$scope.newItem = '';
$scope.addItem = function(e) {
if (e.which === 13) {
hold.holdAddList($scope.newItem);
$scope.newItem = '';
}
};
$scope.displayHold = hold.holdGetList();
$scope.rmList = function(item) {
//get index of displayHold
$scope.index = $scope.displayHold.indexOf(item);
//add it to historylist
carry.addList($scope.displayHold[$scope.index].item);
//remove displayHold
$scope.displayHold.splice($scope.index, 1);
};
$scope.modalItem = function(item){
$scope.index = $scope.displayHold.indexOf(item);
return $scope.displayHold[$scope.index].item;
};
}]);
app.controller('secondCTRL', ['$scope', 'carry', function($scope, carry) {
$scope.controller2Ar = carry.getList();
}]);
第二.html
<header>
<ul class="nav nav-tabs">
<li role="presentation">
<a href="#/">Home</a>
</li>
<li role="presentation" class="active">
<a href="#/second">History</a>
</li>
</ul>
</header>
<div id="mainBox">
<ul ng-repeat="x in controller2Ar" class="list-group" id="repeatBoxAlt">
<li class="list-group-item col-xs-12 col-sm-8 col-md-4 col-lg-4 col-centered lead strike">
<center>
<h7>{{x.item}}</h7>
</center>
</li>
</ul>
</div>
这是索引.html
<!DOCTYPE HTML>
<html ng-app="app">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Roboto:400,500' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://bootswatch.com/paper/bootstrap.min.css"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="main.css"/>
</head>
<body>
<div ng-view></div>
<script src="node_modules/angular/angular.min.js"></script>
<script src="node_modules/angular-route/angular-route.min.js"></script>
<script src="app.js"></script>
</body>
</html>
这是因为您的模态没有唯一的 id 和触发器:
将{{index}}
或唯一标识符添加到模态 ID 和触发器以显示模态。
<header>
<ul class="nav nav-tabs">
<li role="presentation" class="active">
<a href="#/">Home</a>
</li>
<li role="presentation">
<a href="#/second">History</a>
</li>
</ul>
</header>
<center>
<div>
<div class="form-group">
<div class="col-xs-12 col-sm-8 col-md-4 col-lg-4 col-centered" id="nv">
<div class="row"></div>
<input ng-model="newItem" ng-keypress="addItem($event)" type="text" class="form-control lead" id="nv" placeholder="Add a grocery.">
</div>
</div>
</div>
</center>
<div id="mainBox">
<ul class="list-group checked-list-box" id="repeatBox">
<li class="list-group-item col-xs-12 col-sm-8 col-md-4 col-lg-4 col-centered" id="liBox" ng-repeat="x in displayHold track by $index" >
<input type="checkbox" ng-model="deleted"></input>
<h7 id="lItem" class="lead" ng-class="{strike: deleted,notActive: deleted}">{{x.item}}</h7>
<button type="button" class="btn btn-default pull-right" ng-click="rmList(x)">
<span class="glyphicon glyphicon-trash" id="icon"></span>
</button>
<button type="button" class="btn btn-default pull-right" data-toggle="modal" data-target="#myModal{{index}}">
<span class="glyphicon glyphicon-plus" id="icon"></span>
</button>
<div class="container">
<div id="myModal{{index}}" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="btn btn-default" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">{{modalItem(x)}}</h4>
</div>
<div class="modal-body lead">
<p>{{displayHold.x}}</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
相关文章:
- 平均值:无法将数据更新到数据库
- d3在数据更新时错误地附加了dom元素
- 使用角度传递的数据更新模态
- jQuery使用XML数据更新UL列表
- ng表DOM未使用数据更新进行更新
- 在线/离线数据更新移动最佳实践
- 使用 预测 API 数据更新的 HTML 模板
- JQuery 数据表列数据更新工作太慢
- 使用Plotly中的新数据更新图形的高性能方法
- 使用新数据更新 D3 饼图.json
- jQuery - 使用相同的数据更新 2 个或多个表单字段
- 如何在数据更新时更新可重用的 d3.js 图形
- 组合框/下拉框在数据更新后未更新
- 在 JavaScript 中的数据更新后,我的 DOM 对象不会更新
- 如何使用来自外部源的数据更新 ng 模型
- 主干.js:使用不同的数据更新集合
- 对数据库进行 AJAX 编辑时,我是否应该立即使用新数据更新接口
- 计划脚本以使用外部数据更新数据库
- 使用从 MongoDB 获取的新数据更新对象属性
- 如何每 n 分钟使用数据更新一次 Web 应用程序