我想要一个按钮被禁用,直到使用FormBuilder for Angular检查复选框.我不想明确检查复选框的值,而是希望使用验证器,以便我可以简单地检查form.valid.
在下面的两个验证案例中,复选框都是
interface ValidationResult {
[key:string]:boolean;
}
export class CheckboxValidator {
static checked(control:Control) {
return { "checked": control.value };
}
}
@Component({
selector: 'my-form',
directives: [FORM_DIRECTIVES],
template: ` <form [ngFormModel]="form" (ngSubmit)="onSubmit(form.value)">
<input type="checkbox" id="cb" ngControl="cb">
<button type="submit" [disabled]="!form.valid">
</form>`
})
export class SomeForm {
regForm: ControlGroup;
constructor(fb: FormBuilder) {
this.form = fb.group({
cb: [ CheckboxValidator.checked ]
//cb: [ false, Validators.required ] <-- I have also tried this
});
}
onSubmit(value: any) {
console.log('Submitted: ', this.form);
} …Run Code Online (Sandbox Code Playgroud) 我有一个构建表单的指令:
app.directive('config', function() {
return {
restrict: 'E',
scope: {
data: '='
},
template: '<form name="configForm">' +
'<input type="number" max="10" ng-model="config.item" name="configItem"/>' +
'<div class="form-error" ng-show="configForm.$error.max">Error</div>' +
'</form>',
controller: 'ConfigDirectiveController',
};
});
Run Code Online (Sandbox Code Playgroud)
我想要做的是通过单元测试验证错误消息将在给定输入时显示.使用角度1.2我可以修改$ scope.config.item并更新视图值并显示错误.
尽管我已经知道,使用angular 1.3,如果模型未通过验证,则视图值不会更新...所以我需要修改视图值以确保显示错误消息.
如何访问"configItem"输入以便我可以设置视图值以确保显示错误消息?
编辑显示单元测试
我看到值设置正确,但错误仍然有一个应用于标签的ng-hide.当我查看页面并手动更改输入值时,将删除ng-hide,如果我输入大于10的内容,则会显示错误.
beforeEach(inject(function($compile, $rootScope) {
element = angular.element('<config data="myData"></config>');
$scope = $rootScope.$new();
$scope.myData = {};
element = $compile(element)($scope);
}));
it('should warn that we have a large number', function() {
var input = element.find('[name="configItem"]')[0];
$scope.$apply(function() {
angular.element(input).val('9000000001');
});
errors = element.find('[class="form-error ng-binding"]');
expect(errors.length).toBe(1);
})
Run Code Online (Sandbox Code Playgroud) 我创建了一个服务来隔离业务逻辑,并将其注入需要信息的控制器.我想要最终做的是让控制器能够观察服务中的值,这样我就不必进行广播/通知或复杂的消息传递解决方案,以便通知所有控制器有关数据的更改.服务.
我创建了一个plnkr,展示了我正在尝试做的基本想法.
http://plnkr.co/edit/oL6AhHq2BBeGCLhAHX0K?p=preview
是否可以让控制器观察服务的价值?
我有一个简单的服务,我试图进行单元测试.无论我尝试什么,searchService都是未知的提供者,或者服务是null(奇怪的是,这不会导致我的测试失败!!).
任何人都可以了解我可能做错了什么?
angular.module('app').service('searchService', function( $q, _ ) { // _ is lodash
var cache = [
{
id: "current",
name: "Current",
description: "Search current data"
},
{
id: "historical",
name: "Historical",
description: "Search historical data"
}
];
this.getSearchOptions = function() {
var deferred = $q.defer();
deferred.resolve( angular.copy( cache ) );
return( deferred.promise );
};
this.getSearchOptionsByID = function( id ) {
var deferred = $q.defer();
var searchOption = _.findWithProperty( cache, "id", id );
if ( searchOption ) {
deferred.resolve( angular.copy( searchOption …Run Code Online (Sandbox Code Playgroud) 我有一个指令,它使用隔离范围将数据传递给随时间变化的指令.它会监视该值的变化,并对每次更改进行一些计算.当我尝试对指令进行单元测试时,我无法触发手表(为简洁而修剪,但基本概念如下所示):
指示:
angular.module('directives.file', [])
.directive('file', function() {
return {
restrict: 'E',
scope: {
data: '=',
filename: '@',
},
link: function(scope, element, attrs) {
console.log('in link');
var convertToCSV = function(newItem) { ... };
scope.$watch('data', function(newItem) {
console.log('in watch');
var csv_obj = convertToCSV(newItem);
var blob = new Blob([csv_obj], {type:'text/plain'});
var link = window.webkitURL.createObjectURL(blob);
element.html('<a href=' + link + ' download=' + attrs.filename +'>Export to CSV</a>');
}, true);
}
};
});
Run Code Online (Sandbox Code Playgroud)
测试:
describe('Unit: File export', function() {
var scope;
beforeEach(module('directives.file'));
beforeEach(inject(function ($rootScope, $compile) …Run Code Online (Sandbox Code Playgroud) 我想有一个简单的代码路径来创建和分派HTTP操作.我想做的是:
this.http.request(...)
.map((res: Response) => res.json())
.catch((err: any) => err.json())
.map((payload: any) => { type: 'SUCCESS', payload })
.catch((payload: any) => { type: 'FAILURE', payload})
.subscribe((action: Action) => this.store.dispatch(action));
Run Code Online (Sandbox Code Playgroud)
这样,成功和失败响应都转换为JSON,然后根据成功/失败标准分配正确的减少类型,以便可以正确地操作商店.(认为用户登录成功和失败,返回200或401).
是否有更清洁或更好的处理方式?当前第二个.catch不能很好地发挥,因为它没有返回一个可观察的.
建议或其他解决方案欢迎?
我有一个简单的e2e测试来验证路由重定向是否有效
<!doctype html>
<html lang="en">
<head>
<title>End2end Test Runner</title>
<script src="../client/components/angular-scenario/angular-scenario.js" ng-autotest></script>
<script src="e2e/scenarios.js"></script>
</head>
<body>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
'use strict';
describe('e2e', function() {
beforeEach(function() {
browser().navigateTo('../../client/index.html');
});
it('should redirect to the main application home page with / is accessed', function() {
browser().navigateTo('#!/');
expect(browser().location().path()).toBe('/app');
});
});
Run Code Online (Sandbox Code Playgroud)
*snip*
files = [
ANGULAR_SCENARIO,
ANGULAR_SCENARIO_ADAPTER,
'./test/e2e/**/*.js',
];
*snip*
Run Code Online (Sandbox Code Playgroud)
当它运行时,browser().location().path()将引发异常:
TypeError: 'undefined' is not a function (evaluating '$document.injector()')
Run Code Online (Sandbox Code Playgroud)
我已经确定它是最终导致问题的.path(),因为如果我执行browser().location()没有引发异常.
但是在浏览器控制台中,这将按预期返回angular.scenario.Future.
为什么会引发异常?
我正试图从v2迁移到gitolite的v3.执行服务器端提交消息检查的旧方法是将检查放在myrepo.git/hooks/update.secondary中的repo特定挂钩中.
在gitolite v3中,他们建议将支票放入VREF.文档有点混乱,我希望澄清.
我在/home/git/.gitolite.rc中添加了以下内容
LOCAL_CODE => "$ENV{HOME}/.gitolite/our_hooks",
Run Code Online (Sandbox Code Playgroud)
在my_hooks目录中,我创建了一个VREF文件夹,并将旧的update.secondary脚本放在那里.在我的gitolite-admin/conf/gitolite.conf文件中,我将以下内容添加到我希望脚本执行的存储库中:
repo myrepo
RW = @my_developers
- VREF/update.secondary = @my_developers
Run Code Online (Sandbox Code Playgroud)
当我尝试推送时,我注意到无法找到VREF/update.secondary.如果我换到
repo myrepo
RW = @my_developers
- /home/git/.gitolite/our_hooks/VREF/update.secondary = @my_developers
Run Code Online (Sandbox Code Playgroud)
我没有得到任何投诉,但钩子似乎根本没有运行.
我是否错过了如何使用gitolite进行额外的更新检查?
我有一个登陆页面,它将显示用户(默认情况下)和"注册"组件,这是一组允许他们注册的输入字段.
对于返回用户,我希望他们按原样查看登录页面,然后单击"登录",只需用登录组件替换注册组件.我不希望URL改变,它应该保持'/'.
对于ui-router我可以做嵌套状态,但不确定Angular2的路由器是否支持呢?
app.ts
@Component({
selector: 'app',
template: '
*snip*
<router-outlet></router-outlet>
*snip*
',
directives: [Footer, ROUTER_DIRECTIVES]
})
@RouteConfig([
{ path: '/...', name: 'Landing', component: LandingComponent, useAsDefault: true },
{ path: '/about', name 'About', component: AboutComponent }
]);
Run Code Online (Sandbox Code Playgroud)
landing.ts
@Component({
selector: 'landing',
template: '
<body>
<div>
<router-outlet></router-outlet>
</div>
</body>',
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{ path: '/', name: 'RegisterForm', component: RegisterForm, useAsDefault: true },
{ path: '/login', name: 'LoginForm', component: LoginForm },
])
Run Code Online (Sandbox Code Playgroud)
着陆组件的路径是否需要不同?
是否有一种可接受的方式来显示slickgrid表中的总行数.我看到有一个寻呼机可以包含在我的页面上,但是附带了分页的附加按钮/设置.
我正在使用没有分组的DataView对象.理想情况下,我希望在视口底部只有一个简单的行,列出表中有多少行.
我可以手动执行此操作,但不确定我是否在Slickgrid API /配置中遗漏了某些内容.
我正在使用基于用户输入的文件编写修改fs.writeFile(...).我最近遇到了一个错误,其中有两个写入请求有效地导致文件被截断.
每个写操作必须按接收顺序完成.但是我不希望用户在继续执行下一个任务之前必须等待写入完成.
排队异步写入的规范方式或流行模块是什么,以便在写入发生时不阻止用户执行其他工作,并保证不发生冲突.
另外,如果可能的话,我希望读取发生在同一个文件中.
我最初的想法是写一个临时文件,然后复制,但这似乎不那么有效.
angularjs ×5
angular ×3
asynchronous ×1
git ×1
gitolite ×1
jasmine ×1
karma-runner ×1
ngrx ×1
node.js ×1
redux ×1
rxjs ×1
slickgrid ×1
unit-testing ×1