此自定义验证指令是官方角度站点上提供的示例. http://docs.angularjs.org/guide/forms 它检查文本输入是否为数字格式.
var INTEGER_REGEXP = /^\-?\d*$/;
app.directive('integer', function() {
return {
require: 'ngModel',
link: function(scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
if (INTEGER_REGEXP.test(viewValue)) {
// it is valid
ctrl.$setValidity('integer', true);
return viewValue;
} else {
// it is invalid, return undefined (no model update)
ctrl.$setValidity('integer', false);
return undefined;
}
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
为了对这段代码进行单元测试,我写道:
describe('directives', function() {
beforeEach(module('exampleDirective'));
describe('integer', function() {
it('should validate an integer', function() {
inject(function($compile, $rootScope) {
var element = angular.element(
'<form name="form">' +
'<input ng-model="someNum" …
Run Code Online (Sandbox Code Playgroud) 我正在使用intelliJ,我的问题是当我开始将一些临时未使用的包导入我的类文件时,intellij会在一秒内删除这些行.
我怎么能关闭这个不那么好的功能呢?
我有一份清单
a = ["a", "b", "c", "d", "e"]
Run Code Online (Sandbox Code Playgroud)
我想在for循环中删除此列表中的元素,如下所示:
for item in a:
print item
a.remove(item)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我能做什么?
我一直在学习AngularJS,关于单元测试的事情一直很顺利,但我已经达到了一个棘手的地方.
假设我有一个简单的表单,例如:
<form name="form">
<input type="text" name="number" ng-pattern="/^d+$/">
</form>
Run Code Online (Sandbox Code Playgroud)
如果我正在测试类似控制器的东西,我知道我会写这样的东西(使用Jasmine + Karma):
beforeEach(module('some.module'));
beforeEach(inject(/* services */) {
/* inject necessary services */
});
it('should be invalid when given bad input', function () {
form.number = 'Not a number';
expect(form.number.$valid).toBeFalsy();
expect(form.$valid).toBeFalsy();
});
Run Code Online (Sandbox Code Playgroud)
但是我不知道我需要注入哪些服务,而且我没有在forms
指南或文档中找到有关单元测试的ng-form
文档.
一个单元如何测试Angular中的表单?
在对用户的git问题进行故障排除时,我不断遇到人们没有注意到来自git的错误/警告消息,然后烧掉他们的手指.有没有办法着色错误和警告git输出?
我想通过标签上的参数将调用发送回指令,然后在指令内适当时调用该方法.例如,单击按钮时,请在父控制器上调用方法.
我有一个简单的plunker它无法正常工作
html文件:
<body ng-controller="ParentController">
<h1> Method Arguments </h1>
<h3> open console to view output</h3>
<hello-world onLoadCallback="myCallback(arg1)"></hello-world>
</body>
Run Code Online (Sandbox Code Playgroud)
javascript文件:
var app = angular.module("app",[]);
function ParentController($scope) {
$scope.myCallback = function(var1){
console.log("myCallback", var1);
}
}
app.directive('helloWorld', function() {
return {
restrict: 'AE',
template: '<h3>Hello World!!</h3>',
scope:{
onLoadCallback: '&'
},
link: function(scope, element, attrs, dateTimeController) {
console.log('linked directive, not calling callback')
scope.onLoadCallback('wtf');
}
};
});
Run Code Online (Sandbox Code Playgroud) 我正在运行Spring 3.1.2应用程序.我有一个RESTful servlet,有许多方法.GET方法工作得非常好(@PathVariables
匹配,响应正确地编组为基于Accept标头的JSON或XML等)100%的时间.
但是POST方法根本不起作用.经过几个小时的捣乱与皈依者以及我能找到的所有其他春天方面(所有修修补补),我把它缩小到required
现场@RequestParam
.这是我用来调查的简化测试方法:
@RequestMapping (value = "/bogus",
method = POST)
public @ResponseBody PassResponse bogus (
@RequestParam (value = "test", required = false) String test) {
// Just some handy garbage objects that marshal to JSON/XML
UserResponse user = new UserResponse ();
user.setName (test);
AccountDetail detail = new AccountDetail (user,null);
return new PassResponse (detail);
}
Run Code Online (Sandbox Code Playgroud)
required = false:一切正常(参数被接收和解释).正如我所期望的那样
required = true :(或未指定,因为这是默认值)我一直收到消息" MissingServletRequestParameterException:Required String parameter'test'不存在 "
客户端视图:
需要= TRUE
Request URL:http://localhost:8080/internal-project/rest/bogus
Request Method:POST …
Run Code Online (Sandbox Code Playgroud) 如何使用角度2在GET调用中发送参数我尝试了以下方式,但得到一些错误说,
Argument of type '{ params: URLSearchParams; }' is not assignable to parameter of type 'RequestOptionsArgs'.
Object literal may only specify known properties, and 'params' does not exist in type 'RequestOptionsArgs'.
Run Code Online (Sandbox Code Playgroud)
功能调用:
import {Http, Response, Headers, RequestOptions, URLSearchParams} from 'angular2/http';
import 'rxjs/Rx';
constructor(private nav: NavController, public http: Http) {
onLogin(value: string): void {
if(this.authForm.valid) {
this.userData.login();
let params = '';
let options = new RequestOptions({
// Have to make a URLSearchParams with a query string
params: new URLSearchParams('validateUsr=false')
});
this.http.get('https://itunes.apple.com/us/rss/topmovies/limit=1/json', …
Run Code Online (Sandbox Code Playgroud) 想象一下在注释或类似的地方打错字:
- // Do thigns
+ // Do things
Run Code Online (Sandbox Code Playgroud)
现在,通过git blame @ -- file
查看提交,该行最初添加在:
decafbad ... // Do things
Run Code Online (Sandbox Code Playgroud)
您可以通过运行以下命令手动修改该行git commit --fixup decafbad
。
有没有办法自动执行此git blame @ -- file |grep thigns
,git commit --fixup decafbad
周期?
我是这个 vue 的新手,当我尝试运行(npm runserve)时出现此错误:
***WARNING Compiled with 4 warnings
warning in ./src/main.js
"export 'default' (imported as 'Vue') was not found in 'vue'
warning in ./src/main.js
"export 'default' (imported as 'Vue') was not found in 'vue'
warning in ./src/main.js
"export 'default' (imported as 'Vue') was not found in 'vue'
warning in ./src/router/index.js
"export 'default' (imported as 'Vue') was not found in 'vue'
Run Code Online (Sandbox Code Playgroud)
应用程序运行于:
索引.js
***WARNING Compiled with 4 warnings
warning in ./src/main.js
"export 'default' (imported as …
Run Code Online (Sandbox Code Playgroud) angularjs ×3
git ×2
unit-testing ×2
angular ×1
jasmine ×1
javascript ×1
karma-runner ×1
list ×1
python ×1
rest ×1
spring ×1
vue.js ×1
vuejs3 ×1