- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 AngularJS v1.3.15。
这是我的 app.js
'use strict'
var userApp = angular.module('userApp', [
'userServices',
'ngRoute',
'LocalStorageModule',
'userCtrls',
'userDirectives',
'angular-ladda'
]);
userApp.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/users', {
templateUrl: 'partials/search.html',
controller: 'userCtrl'
}).
when('/login', {
templateUrl: 'partials/login.html',
controller: 'loginCtrl'
}).
otherwise({
redirectTo: '/users'
});
}]);
userApp.config(["$httpProvider", function ($httpProvider) {
$httpProvider.defaults.transformResponse.push(function(responseData){
convertDateStringsToDates(responseData);
return responseData;
});
}]);
var regexIso8601 = /^(\d{4}|\+\d{6})(?:-(\d{2})(?:-(\d{2})(?:T(\d{2}):(\d{2}):(\d{2})\.(\d{1,})(Z|([\-+])(\d{2}):(\d{2}))?)?)?)?$/;
function convertDateStringsToDates(input) {
// Ignore things that aren't objects.
if (typeof input !== "object") return input;
for (var key in input) {
if (!input.hasOwnProperty(key)) continue;
var value = input[key];
var match;
// Check for string properties which look like dates.
if (typeof value === "string" && (match = value.match(regexIso8601))) {
var milliseconds = Date.parse(match[0])
if (!isNaN(milliseconds)) {
input[key] = new Date(milliseconds);
}
} else if (typeof value === "object") {
// Recurse into object
convertDateStringsToDates(value);
}
}
}
服务.js
'use strict';
/* Services */
var userServices = angular.module('userServices', ['ngResource']);
userServices.factory('User', ['$resource',
function ($resource) {
return $resource('user/:studentNumber', {
studentNumber: '@studentNumber'
}, {
query: {
method: 'GET',
isArray: true
},
update: {
method: 'PUT',
params: {
validUntil: '@validUntil'
}
}
});
}]);
Controller .js
'use strict';
/* Controllers */
var userCtrls = angular.module('userCtrls', []);
userCtrls.controller('userCtrl', ['$scope', '$location', 'User', '$http', 'localStorageService', function ($scope, $location, User, $http, localStorageService) {
$scope.users = User.query(function (users) {
// when I breakpoint in here, users are a proper array and all dates are Date objects. everything seems fine.
}, function () {
//failure
$location.path('/login');
});
}]);
运行应用程序后,我在控制台中收到此错误
TypeError: get is not a function
at Array.<anonymous> (http://localhost:1337/js/bower-components/angular/angular.js:17717:24)
at comparator (http://localhost:1337/js/bower-components/angular/angular.js:17724:36)
at Array.sort (native)
at http://localhost:1337/js/bower-components/angular/angular.js:17720:30
at $parseFilter (http://localhost:1337/js/bower-components/angular/angular.js:12214:19)
at ngRepeatDirective.compile.trackByIdExpFn (http://localhost:1337/js/bower-components/angular/angular.js:24561:20)
at ngRepeatAction (http://localhost:1337/js/bower-components/angular/angular.js:24619:25)
at Object.$watchCollectionAction [as fn] (http://localhost:1337/js/bower-components/angular/angular.js:14175:13)
at Scope.$get.Scope.$digest (http://localhost:1337/js/bower-components/angular/angular.js:14308:29)
at Scope.$get.Scope.$apply (http://localhost:1337/js/bower-components/angular/angular.js:14571:24)(anonymous function) @ angular.js:11655$get @ angular.js:8596$get.Scope.$digest @ angular.js:14326$get.Scope.$apply @ angular.js:14571done @ angular.js:9698completeRequest @ angular.js:9888requestLoaded @ angular.js:9829
sails.io.js:143
当我评论这一行时它消失了
convertDateStringsToDates(responseData);
该函数的目的是将表示日期的字符串转换为 Date 对象,否则 angular 不会接受它们作为 <input type="date">
的 ng-model
这是我使用 $scope.users 变量的 html 代码
<div class="row" ng-repeat="user in users | filter:query track by user.studentnumber | orderBy:-user.validUntil" style="margin-bottom: 0.5em">
<div class="col-sm-2">
<img
ng-src={{user.photo}}
class="img-thumbnail"/>
</div>
<div class='col-sm-10'>
<div class="form-group">
<h3>{{user.name}}</h3>
<span ng-if="!user.isValid" class="label label-danger">Inactive</span>
<span ng-if="user.isValid" class="label label-success">Active</span>
<h4>{{user.studentNumber}}</h4>
<radio-button-group class="btn-group " data-toggle="buttons-radio" user="user" options="['admin','trainer','user']"></radio-button-group>
<input type="date" ng-model="user.validUntil" ng-change="setValidUntil(user)" class="form-control" placeholder="not specified"/>
<span ng-if="user.updatedBy">User was last updated by the admin <b>{{user.updatedBy}}</b></span>
</div>
</div>
</div>
最佳答案
将 orderBy 表达式放在引号中! orderBy:'-user.validUntil'
关于javascript - $httpProvider 与 ngResource 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30346198/
是否可以向 ngResource 添加虚拟属性? 我创建了这样的服务 app.factory('Person', ['$resource', function($resource) { retur
我正在尝试像这样将 ngResource 安装到工厂中: 'use strict'; var DTapp = angular.module('DeviceTraining'); /* Services
我下面有一个简单的工厂,它请求带有一些位置信息的 json 对象。请求有效,我可以看到数据位于对象中。但不知何故存在范围问题。即使我知道该对象具有该属性和正确的数组,我也无法访问该对象的属性。 您可以
我正在运行此代码来运行 REST 请求 app.factory('GetValidationsService', function($resource) { return $resource(
有哪些选项可以解析 ngResource 响应中的嵌套资源? 有一些关于在 ngResource 中解析嵌套资源的端点的相关问题,但这个问题是关于 REST 响应何时包含嵌套在正在查询的集合中的第二个
我试图在用户与其交互时保持资源列表的最新状态。使用 AngularJS ngResource,我最初使用它的 query 方法获取列表。每个资源都有一个 $remove (或 $delete)方法,对
大家好,我遇到了 ngResource 问题。问题是,当我转到 localhost:3000 时,我得到一个空白页面,而它应该显示登录页面。我在我的 Angular Controller 中声明了 n
我有一个像这样的 ngResource 对象: [...].factory('Event', ['$resource', function ($resource) { return $reso
我正在寻找一种方法来在一个地方处理所有基于 ngResource 的服务的网络相关错误。 From this answer我知道我可以为单个资源中的所有操作定义自定义拦截器。是否可以修改 angula
我有这个代码: dogsResource.delete({id: $stateParams.dogId}, angular.noop, function(value, response
我有具有以下字段的资源: description, picture 是否可以将该资源作为多部分/表单发送到 URL,如果可以,如何发送? 我试过把: app.factory('resource_nam
我有资源,Answer ,其中有一个由 QuestionnaireId 制成的复合键和 QuestionId . ngResource代码如下: function answerResource($re
尝试使用 Angular $resource,但它返回的不是方法。浏览稀疏的文档,我找到了这个: angular.module('productServices', ['ngResource']).
我在 Angular 的资源工厂方面遇到了麻烦。我有路径变量 id,并且 REST API 在同一 url 上也接受名为“id”的查询参数。让我们用一些例子来描述这个问题: 资源网址:"/person
ngResource 看起来是一种在 RESTful(或至少受 REST 启发)Api 中管理数据访问的非常方便的方法...但我想使用它管理的一些数据是只读的(至少从我的应用程序的 Angular 来
我的代码中有以下内容: function store() { this.products = [ new product("sku001", "Apple", "Tasty s
ngResource 看起来是一种在 RESTful(或至少受 REST 启发的)Api 中管理数据访问的非常方便的方法......但我想使用它管理的一些数据是只读的(至少从我的应用程序的 Angul
我正在尝试使用 ngResource 来查询 REST API。我需要在自定义 header 中指定我的 API key 。我试过这样: angular.module('ApiService', ['
我使用以下代码获取数据库对象: // Lookup Project object from server $scope.project = projectModel.get({}, {'id': se
所以我很难理解 angularJs 中的 promises。我混合了我的代码,试图对其进行一些暴力/逆向工程理解,但没有得出任何可行的结论。 我的代码:Is 正在回电以获取我管理的存储库列表。这些只是
我是一名优秀的程序员,十分优秀!