让我先说一下我是AngularJS和CodeIgniter的新手.试图创建单页面应用程序.在中心它显示新闻故事.旁边是提交新故事的表格.我的故事从数据库显示得很好.但是当我尝试从侧面的表单创建新故事时,它会在数据库中输入0.我假设我的模型功能有问题.并且它可能是基本的,因为没有正确编码的信息在MVC之间传递.
的index.php
<div class="span2" ng-controller="NewStoryCtrl">
<h4>New Story</h4>
<form name="newStory">
<label for="title">Title: </label>
<input type="text" name ="title" ng-model="news.title" required>
<label for="text">Text: </label>
<textarea type="text" name="text" ng-model="news.text"required></textarea>
<button ng-click="createNews()">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
controller.js
function NewStoryCtrl($scope, $http) {
$scope.news = {title:$scope.title, text:$scope.text};
$scope.createNews = function(){
$http.post('/ci/index.php/news/create/',$scope.news);
};}
Run Code Online (Sandbox Code Playgroud)
news.php(控制器)
public function create() {
$this->news_model->set_news();
}
Run Code Online (Sandbox Code Playgroud)
news_model.php(型号)
public function set_news() {
$this->load->helper('url');
$slug = url_title($this->input->post('title'), 'dash', TRUE);
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'time' => time(),
'text' => $this->input->post('text')
);
return $this->db->insert('news', $data);
} …Run Code Online (Sandbox Code Playgroud) 我正在使用AngularJS开发单页面应用程序.目前我已经设置好了,所以每5秒钟就会调用一次DB来更新客户端模型.我想用时间戳错开东西.因此它不会从数据库中提取所有内容,而只是提取已经添加的所有内容(如果比上次调用更新).(节省带宽等).但这对我没有意义
角度控制器
var timestamp = '';
function NewsListCtrl($scope, $http, $timeout) {
$scope.news = [];
$scope.newsRequest = function() {
$http.get('/ci/index.php/news/get_news_ajax/' + timestamp).success(function(data) {
timestamp = $.now();
$scope.news = $scope.news.concat(data);
});
};
$scope.newsRequest();
setInterval(function(){
$scope.$apply(function(){
$scope.newsRequest();
});
}, 5000);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码不会在页面上打印任何内容.第一个请求显示对象中的数据.但后续请求(带有时间戳)显示空对象.所以至少第一个请求应该打印出来.
初始请求中的数据样本
{"new":[{"id":"181","title":"not gonna work","slug":"not-gonna-work","time":"1363374669","text":"asdfs","deletetime":null}]}
Run Code Online (Sandbox Code Playgroud)
当我删除
$scope.news = [];
Run Code Online (Sandbox Code Playgroud)
然后我得到错误,说它是未定义的.
有人能够对正确的方法有所了解吗?
编辑:
根据答案,我已将我的代码更新为:
var timestamp = '';
function NewsListCtrl($scope, $http, $timeout) {
$scope.news = [];
$scope.newsRequest = function(){
$http.get('/ci/index.php/news/get_news_ajax/' + timestamp).success(function(data) {
timestamp = $.now();
$scope.news = $scope.news.concat(data.new);
});
};
$scope.newsRequest();
setInterval(function(){ …Run Code Online (Sandbox Code Playgroud) 我有社交媒体统计的对象.我正在尝试将它们重复到表格中.这是我的傻瓜.
HTML:
<table>
<tr ng-repeat="(metric, metricData) in data">
<td>{{metric}}</td>
<td>{{metricData}}</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
控制器对象:
$scope.data = { buzz:0,
Delicious:121,
Facebook:
{
like_count: "6266",
share_count: "20746"
},
GooglePlusOne:429,
LinkedIn:820,
Twitter:4074
};
Run Code Online (Sandbox Code Playgroud)
当我到达Facebook结果时遇到问题.在<td>整个对象中显示(因为它应该与我如何设置我的代码).但我宁愿发生的是重复该对象并显示单元格中的键和值.
我尝试做一些事情,看看是否metricData是一个对象,并做了某种ng-repeat.但我没有运气.关于如何在单元格中显示内部对象(键和值)的任何想法?
<tr class="labels">
<td nowrap="nowrap">Address</td>
<td nowrap="nowrap"><label for="tbAddress"></label>
<textarea name="tbAddress" id="tbAddress" cols="39" rows="5" ng-model="$parent.tbAddress"></textarea>
</td>
</tr>
<tr>
<td> </td>
<td align="right">
<input type="submit" name="button" id="button" value="Submit Record" class="btns" />
<input type="reset" name="button2" id="button2" value="Cancel" class="btns" />
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
我无法textarea使用AngularJS 获得价值.所有input文本字段都通过控制器,除了textarea.我究竟做错了什么 ?
alert($scope.tbAddress);
var thisData = {
'name': $scope.tbFN,
'company': $scope.tbCompany,
'designation': $scope.tbDesignation,
'email': $scope.tbEmail,
'phone': $scope.tbPhone,
'mobile': $scope.tbMobile,
'address': $scope.tbAddress
};
Run Code Online (Sandbox Code Playgroud)
这是警报未定义......