为了发送OAuth2令牌,我在AngularJS上设置默认标头,如下所示:
$http.defaults.headers.common['Authorization'] = 'Bearer ' + access_token;
Run Code Online (Sandbox Code Playgroud)
这工作得很好但我不需要这个标题(我得到一个错误)一个特定的请求.
有没有办法在执行该请求时排除默认标头?
谢谢!
感谢Riron让我走上了正确的道路.这是答案:
$http({
method: 'GET',
url: 'http://.../',
transformRequest: function(data, headersGetter) {
var headers = headersGetter();
delete headers['Authorization'];
return headers;
}
});
Run Code Online (Sandbox Code Playgroud) 我使用ng-form作为父表单,使用ng-form进行ng-repeat验证.
<div ng-form="form">
<div ng-repeat="field in fields">
<div ng-form="subform"><input...></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
并验证如下:
if ($scope.form.subform.$invalid && !$scope.form.subform.$error.required) {
// Do something...
}
Run Code Online (Sandbox Code Playgroud)
(这是非常简化的示例,我为不同的输入类型和不同的名称提供了更多不同的子表单,例如input [text]被命名为TextForm,input [numeric]是NumericForm等.)
如果只有现场,一切都按预期工作.但是,如果ng-repeat生成多个字段,则验证仅触发最后一个子表单,其他子表单将被忽略.
有没有办法循环遍历所有子表单,以检查其中一个是否无效?
此外,我正在标记所有未填写的必填字段,如下所示:
if ($scope.form.$error.required) {
angular.forEach($scope.form.$error.required,
function (object, index) {
$scope.form.$error.required[index].$setDirty();
}
);
}
Run Code Online (Sandbox Code Playgroud)
所以,如果我的字段是这样完成的:
....ng-form="TextForm" ng-class="{ 'has-error': TextForm.$dirty && TextForm.$invalid }"....
Run Code Online (Sandbox Code Playgroud)
并且它标记所有子表单,即使存在许多具有相同名称的子表单.
也许我可以用无效字段做类似的事情?虽然尝试了很多东西但没有任何效果
将Leaflet添加到我的AngularJS应用程序后:
<leaflet id="map" defaults="defaults" center="center" bounds="bounds" controls="controls" event-broadcast="events"></leaflet>
Run Code Online (Sandbox Code Playgroud)
并设置:
// Initialise the feature group to store editable layers
var drawnItems = new L.FeatureGroup();
// Initialise the draw control
var drawControl = new L.Control.Draw({
position: 'topright',
edit: {
featureGroup: drawnItems
}
});
// Configure Leaflet
angular.extend($scope, {
defaults: {
zoomControlPosition: 'topright',
minZoom: 3,
tileLayerOptions: {
detectRetina: true,
reuseTiles: true,
attribution: '<a href="http://osm.org">OpenStreetMaps</a>'
}
},
center: {},
controls: {
custom: [drawControl]
},
events: {
map: {
enable: ['click']
}
}
});
Run Code Online (Sandbox Code Playgroud)
遵循此代码,它不会被评估(尽管没有显示错误): …
在我的代码中有一行:
var contentWidth = angular.element(document.querySelector('.content'))[0].clientWidth;
Run Code Online (Sandbox Code Playgroud)
它在运行应用程序时工作正常但在单元测试时出现错误:
TypeError: 'undefined' is not an object (evaluating 'angular.element(document.querySelector('.content'))[0].clientWidth')
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
我已经创建了一个在AngularJS中显示嵌套注释的指令.它的主要观点是,当从API返回的JSON看起来像这样时,会有无限的注释:
[
{
'id': 66,
'text': 'This is the first comment.',
'creator': {
'id': 52,
'display_name': 'Ben'
},
'respondsto': null,
'created_at': '2014-08-14T13:19:59.751Z',
'responses': [
{
'id': 71,
'text': 'This is a response to the first comment.',
'creator': {
'id': 14,
'display_name': 'Daniel',
},
'respondsto': 66,
'created_at': '2014-08-14T13:27:13.915Z',
'responses': [
{
'id': 87,
'text': 'This is a response to the response.',
'creator': {
'id': 52,
'display_name': 'Ben',
},
'respondsto': 71,
'created_at': '2014-08-14T13:27:38.046Z',
'responses': []
}
]
}
]
},
{
'id': …
Run Code Online (Sandbox Code Playgroud) 在我的AngularJS应用程序中,我只需保存浏览器历史记录中的状态更改.这就是为什么当我更改$ location的参数时,我正在使用replace()方法.
例如,当我访问/ page1时,它将保存在历史记录中.使用replace()自动添加'center'参数,因此它不会添加新的历史记录条目:
$location.search('centre', hash).replace();
Run Code Online (Sandbox Code Playgroud)
每次移动地图时,"中心"都会发生变化.
当我转到/ page2时,会创建历史记录中的新条目.当我移动地图时,"中心"会发生变化.
问题是,当我按下BACK按钮时,我将转到/ page1,但我需要保持'center'与之前相同,但它会更改为与/ page1的历史记录条目一起保存的内容.
我该如何解决这个问题?
我试着添加:
$window.history.replaceState({}, '', $location.absUrl());
Run Code Online (Sandbox Code Playgroud)
我做replace()后,但没有工作.
我可以说,AngularJS在HTML中做了一些神奇的事情.
我试图直接在HTML中实现"剥离描述".到目前为止,我设法限制显示的字符并添加三个点.
<div ng-init="string = 'My string is cool'">
{{ string | limitTo: 10 }}<span ng-show="string.length > 10">...</span>
</div>
Run Code Online (Sandbox Code Playgroud)
但是如果字符串(在限制之后)以空格结尾,我最终会有"结束......"而不是"结束......".有没有办法直接在HTML中删除最后一个空格?