- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不能强制 Angular 在更新输入时触发 $parsers.push 所以,在上面的测试中有这个指令,你如何触发 $parsers.push ?
mainApp.directive('amountConverter', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModelController) {
var isInt = function(value) {
return !isNaN(parseInt(value, 10)) && (parseFloat(value, 10) == parseInt(value, 10));
},
convert = function(initial, multiplier, text) {
var amount = text.replace(initial, "");
if (!isInt(amount)) {
throw " Amount not a number";
}
return amount * multiplier;
};
//convert data from view format to model format
ngModelController.$parsers.push(function(value) {
if (value === null || value === undefined) {
return null;
}
var text = value.toUpperCase();
var amount = 0;
var initial = text.substring(text.length - 1);
if (initial === "B") {
amount = convert(initial, 1000000000, text);
} else if (initial === "M") {
amount = convert(initial, 1000000, text);
} else if (initial === "K") {
amount = convert(initial, 1000, text);
} else if (initial === "T") {
amount = convert(initial, 1000, text);
} else {
return value;
}
element[0].value = amount;
return amount; //converted
});
}
};
});
describe('Directives', function () {
var element, scope;
beforeEach(module('mainApp'));
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope;
element = angular.element('<input type="text" data-ng-model="Amount" amount-Converter/>');
$compile(element)(scope);
scope.$digest();
}));
//////Jasmine Test
describe('amountConverter', function () {
it('should return change element state after click to be visible', function () {
element.scope().Amount = '10k';
element.scope().$apply();
expect(element[0].value).toBe('10000');
});
});
});
最佳答案
摘自阿科斯塔评论中的视频:
使 Angular 发挥作用的方法是触发元素上的“输入”事件(使用 element.trigger('input')
。因此在您的测试中,您应该能够做:
element.val("10k");
element.trigger("input");
element.scope().$apply(); //not sure if you even need to do this, video says yes, however on angular 1.3.8 I didnt need to
expect(element.val()).toBe('10000');
expect(element.scope().Amount).toBe('10000');
关于angularjs - 如何使用 jasmine 在 Angular 上测试 $parsers.push 和 $formatters.push,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374089/
我向我的记录器添加了两个具有不同格式化程序的处理程序。第一个需要继承 logging.Formatter 来进行自定义格式化。默认格式化程序将满足第二个处理程序的需要。 假设第一个格式化程序只是从消息
我尝试运行一个简单的 .feature 文件以生成 Step 文件所需的代码,但返回以下异常: 下面是pom文件: org.seleniumhq.selenium
userA 在法国,他们将某样东西的价格定为 10 欧元,我获取他们的货币信息,并使用 Locale.current.identifier 将其本地区域的副本记录到数据库,这给出了我 € 并将其保存到
我已经在 android 项目上集成了 checkstyle。但是我收到以下错误: > Unable to create listeners: formatters {[com.puppycrawl.
这个解决方案对我不起作用: Running a spec in RubyMine results in "cannot load such file -- teamcity/spec/runner/f
我正在尝试在 Eclipse 中设置我的第一个 Cucumber。我按照教程视频建议的方式在 Features 文件夹中设置了一个基本功能文件,但尝试作为 cucumber 功能执行时,我得到以下信息
操作系统:Arch Linux,Rails 版本:4,RubyMine:6.3 当我从 Tools - Run Rake Task - spec 运行规范时,我总是会收到此错误: /home/chyl
我正在尝试使用 formattable Awesome 包并获得一个表格,其中包含在多列上缩放的百分比和颜色。 这里是代码 set.seed(123) df % mutate_if(is.nume
我试图将字符串定位在一行上的某个起始位置,无论前一个文本在哪里结束。例如: Mandatory arguments to long options are mandatory for short op
因此,我将以下代码写入文件: Formatter output = ....... // Creating the formatter works, writes to appropriate fil
我知道有很多 HTML 格式化程序。我已经尝试了很多,但没有一个符合我的要求。请建议您是否遇到了可以: 1) 转换 BBCC 进入 BB
在本文中,我们将介绍 Java Formatter.format 方法。 java.util.Formatter 类用作 printf 样式格式字符串的解释器。 Formatter 可以执行布局对齐、
我有一个简单的验证器,可以根据正则表达式检查输入: .directive('test', function () { return { restrict: 'A', require:
假设您有一些表格数据,其中数据长度可能会有所不同,Formatter 类是否有自动调整填充的规定? 所以不是这个(注意 A 列): columnA columnB 1
我有一个这样注释的模型: [DataMember(EmitDefaultValue = false)] [JsonProperty("extension_{0}_name")] public stri
示例数据(修改自 formattable github 文档): df <- data.frame( id = 1:10, name = c("Bob", "Ashley", "James",
我编写了下面的代码,它创建了一个文件并完美地写入它,但我想在输出中查看文件的内容,但我只收到此消息:“java.io.BufferedWriter@140e19d”。我不明白!谁能向我解释一下为什么我
我有兴趣使用 formattable R 包中的工具,但我只想在表格中显示有更改的地方。也就是说,我想要通过 collapse_rows() 函数在 kableExtra 包中提供分层行标签。 例如,
我已经开始使用 Intellij (之前使用过 Eclipse),但我似乎无法模仿 Eclipse 格式 Eclipse Code Formatter 插件完成了大部分工作,但仍然存在一些差异。 例如
我是编码新手,有一个问题如何修复此代码以实际使用用户输入作为创建的文件的新名称。如您所见,我希望在格式化程序中使用字符串“文件名”(例如,输入是“Example.txt”,这应该在格式化程序的构造函数
我是一名优秀的程序员,十分优秀!