【发布时间】:2018-03-24 19:55:58
【问题描述】:
我在数据库表中有一些数据,我使用 Angular 来显示它。我使用以下脚本在页脚中来做到这一点:
<script !src="">
var app = angular.module('enterprise', []);
app.controller('entercontroller', function($scope, $http){
$scope.loadData= function(){
$http.post('<?php echo base_url(); ?>Groups_/load_data')
.then(function(mydata){
console.log(mydata);
$scope.datas = mydata.data;
});
};
$scope.loadData();
});
</script>
我的控制器中的 load_data() 函数如下:
public function load_data(){
$data["groups"] = $this->Groups->fetch_groups();
return $this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output(json_encode($data["groups"], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
我的观点是一些带有提交按钮的输入。我需要的是当我单击按钮添加新记录时,表格会自动更新,而无需每次刷新页面。所以我制作了另一个脚本来这样做
<script>
$(document).ready(function() {
$('#add').on('submit', function() {
var that = $(this);
$dataString = that.serialize();
$.ajax({
url: '<?php echo base_url(); ?>'+ $("#uri").val() ,
type: 'POST',
dataType: 'json',
cache:false,
data : $dataString,
success: function (data) {
console.log(data);
$('#message').html(data['message']);
$(':input','#add')
.removeAttr('checked')
.removeAttr('selected')
.not(':button, :submit, :reset, :hidden, :radio, :checkbox')
.val('');
},
error: function (data) {
console.log('error');
console.log(data);
}
});
return false;
});
});
</script>
HTML 代码如:
<div class="page-wrapper" ng-app="enterprise" ng-controller="entercontroller">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-block">
<form class="floating-labels m-t-40" method="post" id="add" action="addgroup">
<div class="form-group m-b-40">
<input type="text" name="group_title" class="form-control input-lg" id="input8" required><span class="bar"></span>
<label for="input8">Group Name</label><br />
</div>
<div class="form-group m-b-40">
<input type="text" name="group_link" class="form-control input-lg" id="input7" required><span class="bar"></span>
<label for="input7">Link</label>
</div>
<div class="form-group m-b-40">
<select name="group_icon_code" class="custom-select form-control" id="location1" required >
<option value="">Icon</option>
<?php
if(isset($icons))
foreach($icons as $icon)
echo '<option value="'.$icon->icon_value.'">'.$icon->icon_name.'</option>';
?>
</select>
</div>
<div class="text-xs-right">
<input type="hidden" id="uri" name="uri" value="Groups_/addgroup" />
<button type="submit" ng-click="loadData()" name="add" value="1" class="btn btn-success"> <i class="fa fa-check"></i> save</button>
<button type="reset" class="btn btn-inverse"> <i class="fa fa-times"></i> cancel</button>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-block">
<div class="table-responsive">
<table class="table color-bordered-table inverse-bordered-table">
<thead>
<tr>
<th>title</th>
<th>link</th>
<th>order</th>
<th>icon</th>
<th class="text-nowrap">control</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="d in datas">
<td>{{ d.group_title }}</td>
<td>{{ d.group_link }}</td>
<td>{{ d.group_order }}</td>
<td><i class="mdi {{ d.group_icon_code }}"></i></td>
<td class="text-nowrap">
<a href="#" data-toggle="tooltip" data-original-title="Edit"> <i class="fa fa-pencil text-inverse m-r-10"></i> </a>
<a href="#" data-toggle="tooltip" data-original-title="Delete"> <i class="fa fa-close text-danger"></i> </a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
我曾尝试将 Angular 脚本放在点击功能中,但它不起作用。 我需要的是在添加后再次重新加载相同的位置。 任何想法。
【问题讨论】:
-
首先准备api,然后准备view,在那里进行ng-repeat,然后,发出api请求放东西,api请求获取数据并放回模型。不需要 jQuery。看看任何一个待办事项应用程序,它是如何制作的。
标签: javascript jquery angularjs codeigniter