我在这里看到了Angular select指令的文档:http://docs.angularjs.org/api/ng.directive:select .我无法想象如何设置默认值.这令人困惑:
选择作为数组中值的标签
这是对象:
{
"type": "select",
"name": "Service",
"value": "Service 3",
"values": [ "Service 1", "Service 2", "Service 3", "Service 4"]
}
Run Code Online (Sandbox Code Playgroud)
html(工作):
<select><option ng-repeat="value in prop.values">{{value}}</option></select>
Run Code Online (Sandbox Code Playgroud)
然后我试图在select元素中添加一个ng-option属性来设置prop.value为默认选项(不工作).
ng-options="(prop.value) for v in prop.values"
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在下载角度,角度引导和带凉亭的自举.Bootstrap依赖于安装在进程中的jquery.但我在我的项目中不需要它,因为我只使用bootstrap的CSS.
所以我试图永久删除对jquery的依赖
bower uninstall jquery --save
Run Code Online (Sandbox Code Playgroud)
它正在卸载jquery,但下次我制作时bower update,它会再次下载.
有没有办法让bower永久地跳过依赖?
编辑:我希望有这样的事情:
"resolutions": {
"jquery": "no, thanks"
}
Run Code Online (Sandbox Code Playgroud) $location.search() 设置网址
传递给的搜索参数search()由表单设置.选择和取消选择元素会导致某些参数值为"null",因此url如下所示:
myapp.com/search?type=text¶meter=null
Run Code Online (Sandbox Code Playgroud)
所以我想从网址中删除那些"null"参数.在文档中,有一个"paramValue",它可以作为第二个参数传递给.search(search,paramValue):如果值为null,则该参数将被删除.
但我不能做这项工作......有什么建议吗?
编辑:这是基于@BKM解释的解决方案
要删除所有参数,null必须循环遍历所有参数并测试每个参数,如下所示:
for (var i in search) {
if (!search[i]) $location.search(i, null);
}
$location.search(search);
Run Code Online (Sandbox Code Playgroud) 使用$ http,我们可以这样做:
var config = { headers: { 'something': 'anything' } };
$http.get('url/to/json', config)
.success(function() {
// do something…
})
Run Code Online (Sandbox Code Playgroud)
我想用$ resource引用(不工作)做同样的事情:
var config = { headers: { 'something': 'anything' } };
MyResource.get(
config,
function() { // success
// do something…
}
);
Run Code Online (Sandbox Code Playgroud)
使用相应的服务声明如下:
.factory('MyResource', function($resource){
return $resource('url/to/json');
})
Run Code Online (Sandbox Code Playgroud)
它不起作用:config对象转到url而不是http头.
有没有办法做到这一点 ?
角文档大约$interval是说:
注意:完成后,必须明确销毁此服务创建的间隔.
但它没有解释如何破坏$ interval.
例如,我有一个包含此代码的指令:
$interval(function() {
for (var i in myArray) {
// do domething
}
}, 5000);
Run Code Online (Sandbox Code Playgroud)
例如,当用户更改页面时,如何销毁它?
我正在尝试使用ui-bootstrap 0.6的模态指令
这是ui-bootstrap页面中的工作默认plunker:
http://plnkr.co/edit/JGBiBSeRqOnwRhYA9py8?p=preview
现在,我试图使编码风格适合angular-seed风格,将其包含在一个应用程序中,如下所示:
http://plnkr.co/edit/Y59rwlcNpQdijKtmjOPy?p=preview
angular.module('MyModal', ['ui.bootstrap', 'MyModal.controllers']);
angular.module('MyModal.controllers', [])
.controller('ModalDemoCtrl', [ '$scope', '$modal', '$log', function ($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function () {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
items: function () {
return $scope.items;
}
}
});
modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
}])
.controller('ModalInstanceCtrl', [ '$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) { …Run Code Online (Sandbox Code Playgroud) 在checkJs启用了 VsCode 的 nodej 项目中,当需要 json 文件时,例如
const myFile = require('./my-file.json')
Run Code Online (Sandbox Code Playgroud)
这会导致错误[ts] Cannot find module。
如何消除错误警告?
我试过了:
添加"resolveJsonModule": true到compilerOptionsin jsconfig.json,但它不起作用。
创建一个typing.d.ts包含以下内容的文件:
declare module '*.json' {
const value: any;
export default value;
}
但是现在出现了一个错误 [ts] Type 'typeof import("*.json")' must have a '[Symbol.iterator]()' method that returns an iterator. [2488]
我试图在$ scope和$ scope.$ broadcast的两个控制器之间共享信息.
这是观点:
<div ng-controller="ParentCtrl">
<div ng-controller="ChildCtrl">
{{content}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
和控制器:
.controller('ParentCtrl', ['$scope', function($scope) {
$scope.$broadcast('someEvent', 'bidule');
}])
.controller('ChildCtrl', ['$scope', function($scope) {
$scope.$on('someEvent', function(event, b) {
$scope.content = b;
});
}])
Run Code Online (Sandbox Code Playgroud)
和掠夺者
我在这做错了什么?
在html视图中,图像显示如下:
<img ng-src="{{element.image.url}}">
Run Code Online (Sandbox Code Playgroud)
element.image.url指向一个网址:/rest_api/img/12345678.
这工作正常,显示图像.
现在,我添加身份验证:
在对用户进行身份验证之前,每个资源都会响应一个http错误401,图像也是如此.验证成功后,会将令牌放在自定义标头中,并与每个$ http请求一起发送,以允许访问资源:
$http.defaults.headers.common['Authorization'] = token;
Run Code Online (Sandbox Code Playgroud)
这适用于加载$ resource的Json文件.但是在认证之后,到图像的直接链接仍然是401.
如何使用自定义标题调用图像?
或者关于我应该怎么做的任何建议.
我试图显示一个包含大量元素的表.我想对表进行分页并仅加载当前页面上显示的元素.现在,json已经加载了$resource.
我在这里读到,在json头中传递分页信息(currentPage,pagesCount和elementsCount)是个好主意.
如何从角度访问json头中的那些信息?
这是基本的js结构:
angular.module('App.services', ['ngResource']).
factory('List', function($resource){
return $resource('url/of/json/:type/:id', {type:'@type', id:'@id'});
});
angular.module('App.controllers', []).
controller('ListCtrl', ['$scope', 'List', function($scope, List) {
var $scope.list= List.query({offset: 0, limit: 100, sortType: 'DESC' });
}]);
Run Code Online (Sandbox Code Playgroud)