我正在使用通过AngularJS在我的控制器中发送一些json
respond_to :json
def create
respond_with Task.create(description: params[:description])
end
Run Code Online (Sandbox Code Playgroud)
将tasks被存储在数据库中,但后来我在我的日志得到这个消息
ActionController::UnknownFormat - ActionController::UnknownFormat:
(gem) actionpack-4.0.0/lib/action_controller/metal/mime_responds.rb:372:in `retrieve_collector_from_mimes'
(gem) actionpack-4.0.0/lib/action_controller/metal/mime_responds.rb:327:in `respond_with'
Run Code Online (Sandbox Code Playgroud)
...
我试着添加我的路线resources :tasks, :defaults => {:format => "js"}.我该如何处理这个问题?
我正在考虑Angular的CRUD通用工厂(我目前优先使用服务):
app.factory('dataFactory', ['$http', function ($http) {
var urlBase = '/odata/ContentTypes';
// The _object_ defined in the factory is returned to the caller, rather than as with a service,
// where the _function_ defined in the service is returned to the caller
var dataFactory = {};
dataFactory.getContentTypes = function () {
var contentTypes = $http.get(urlBase);
return contentTypes;
};
dataFactory.getContentType = function (id) {
return $http.get(urlBase + '/' + id);
};
dataFactory.insertContentType = function (contentType) {
return $http.post(urlBase, contentType);
};
dataFactory.updateContentType = …Run Code Online (Sandbox Code Playgroud) 我有这个简单的控制器标记
<div ng-controller="TestCtrl" ng-show="isVisible()">
<input type="text" ng-model="smth"/><br>
<span>{{smth}}</span>
</div>
Run Code Online (Sandbox Code Playgroud)
和控制器本身
function TestCtrl($scope, $log)
{
$scope.smth = 'smth';
$scope.isVisible = function(){
$log.log('isVisible is running');
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么在模型的每一个小变化之后(例如在文本框中更改一个字母)我可以isVisible is running在控制台中看到?这种情况不是问题,但我认为它将在大型应用中.我可以避免这个吗?
我有一个奇怪的问题.我在我的应用程序中使用AngularJS,我遇到了标记问题.我有一个控制器,用我的元素处理城市和州:
function MeuController($scope, $http) {
$scope.states = [];
$scope.selectedState = '';
$scope.cities = [];
$scope.selectedCity = '';
$http.get('/state/GetStates').success(function(result) {
$scope.states = result;
});
$scope.getCities = function() {
$http.get('/cities/GetCitiesByState?state=' + $scope.selectedState).success(function(result) {
$scope.cities = result;
});
}
});
Run Code Online (Sandbox Code Playgroud)
在这一点上,一切都很好,易于理解.但...
当我以这种方式创建元素时:
<select class="span2" name="SelectedState" ng-model="selectedState"
ng-change="getCities()" ng-options="state.ID as state.Name for state in states">
<option></option>
</select>
<select class="span6" name="SelectedCity" ng-model="selectedCity"
ng-options="city.ID as city.Name for city in cities">
<option></option>
</select>
Run Code Online (Sandbox Code Playgroud)
......我的元素没有填充.
如果我这样试试:
<select class="span2" name="SelectedState" ng-model="selectedState"
ng-change="getCities()">
<option ng-repeat="state in states" value="state.ID">{{state.Name}}</option>
</select>
<select …Run Code Online (Sandbox Code Playgroud) 所以为了简单起见,我试图用我从ajax调用获得的新项目列表更新我的选择列表.列表中有项目.我将模型设置为新列表并执行$ scope.$ apply().这在firefox中很有用,但在IE中却不行.我究竟做错了什么?我缺少一些IE9的东西吗?(我一直在寻找几个小时,准备放弃).感谢我能得到的所有帮助.
HTML:
<select
ng-model="SelectedParameter"
ng-options="Parameter.Name for Parameter in AllParameters">
</select>
Run Code Online (Sandbox Code Playgroud)
JS:
$.getJSON("/Cont/GetList", {id: id},
function (result) {
var allParameters = result.Data.AllParameters;
$scope.AllParameters = allParameters;
$scope.$apply();
}
);
Run Code Online (Sandbox Code Playgroud) 现在我知道,由于javascript的执行方式,建议您将所有远程请求作为异步而不是同步运行.虽然我同意99%的时间,但有时您确实希望将远程请求作为同步而不是异步运行.例如,加载会话数据是我想要同步进行的操作,因为我不希望在加载数据之前呈现任何视图.这个plunker显示异步加载会话数据的问题(注意:我使用$ timeout来模拟异步调用会发生什么):
http://plnkr.co/edit/bzE1XP23MkE5YKWxRYrn?p=preview
data属性不会加载任何内容,因为数据在尝试获取数据时不可用而data2只是因为数据在尝试获取数据时可用.现在,在这种情况下,我可以将会话变量放在范围上并完成它,但情况并非总是如此.
除了使用jQuery的.ajax()方法(试图尽可能少地依赖jQuery)之外,有没有更好的方法在角度应用程序中同步远程调用?
我已经有一个基于Codeigniter的RESTful api服务,它对mySQL数据库执行基本的CRUD操作.现在我正在尝试用angularJS和Restangular制作一个控制面板.
我发这样的POST请求:
HTML:
<form ng-submit="addUser()">
<input type="text" name="name" ng-model="newUser.name" placeholder="Enter Your Name" />
<input type="text" name="fact" ng-model="newUser.fact" placeholder="Enter A Fact" />
<input class="pure-button" type="submit" value="Send" />
</form>
Run Code Online (Sandbox Code Playgroud)
JS:
$scope.addUser = function ()
{
var newuser = $scope.newUser;
Restangular.one("user").post(newuser).then(function(data) {
console.log(data);
})
}
Run Code Online (Sandbox Code Playgroud)
PHP(使用Codeigniter的inpu类):
$data = array(
'name' => $this->input->post('name'),
'fact' => $this->input->post('fact')
);
Run Code Online (Sandbox Code Playgroud)
然后它将$ data数组写入数据库.但在数据库中,值始终为0,这意味着$ data ['name']和$ data ['fact']为空.
我想我应该在PHP中以不同的方式处理数据,但我该怎么做呢?或者POSTed数据的类型是什么?
尝试在具有隔离范围的指令上使用过滤器:
<div tags="p.tags | refTags"></div>
Run Code Online (Sandbox Code Playgroud)
在$ digest循环中导致无限循环:
当应用程序的模型变得不稳定并且每个$摘要周期触发状态更改和随后的$摘要周期时,会发生此错误.Angular检测到这种情况并防止无限循环导致浏览器无响应.
.directive 'tags', ->
restrict: 'A'
scope:
tags: '='
templateUrl: 'partials/tags.html'
.filter 'refTags', ->
(tags) ->
['a filtered', 'array of values']
Run Code Online (Sandbox Code Playgroud)
谐音/ tags.html
<ul>
<li ng-repeat="tag in tags">{{tag.tag}}</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
p.tags在控制器中
p.tags = ['HTML5', 'CSS', 'JavaScript', 'Angular JS', 'Backbone JS', 'Node JS', 'SASS + Compass', 'Oragami', 'Running', 'Cat Food', '#catfood']
Run Code Online (Sandbox Code Playgroud)
这种行为是否正常?
angularjs ×7
javascript ×2
ajax ×1
codeigniter ×1
coffeescript ×1
factory ×1
php ×1
restangular ×1
select ×1