-1
ディレクティブのユニットテストを作成しようとしていて、次のエラーが発生しました。角度ディレクティブのユニットテスト
は、変数を見つけることができません:
$compile
変数を持っていないので$
HTML
<html ng-app="plunker">
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css">
<script>
document.write("<base href=\"" + document.location + "\" />");
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>
<script src="app.js"></script>
</head>
<body>
Align number to right
<br/>
<mi-format-number-in-grid numbervalue="11.7866" scale="1">11.097866</mi-format-number-in-grid>
<br/>
<mi-format-number-in-grid numbervalue="11.097866"></mi-format-number-in-grid>
<br/>
<mi-format-number-in-grid numbervalue="11.097866"></mi-format-number-in-grid>
<br/>
<mi-format-number-in-grid numbervalue="11.097866"></mi-format-number-in-grid>
<br/>
</body>
</html>
JS
var app = angular.module('plunker', []);
app.directive('miFormatNumberInGrid', function($compile) {
return {
restrict: 'E',
scope: false,
link: function(scope, element, attributes) {
var digit = 0;
var cssClass = "";
var pctValue = parseFloat(attributes.numbervalue);
var scale = parseInt(attributes.scale);
if (attributes.percentchar === "true")
cssClass = "class='mi-percentsuffix'";
if (!isNaN(scale))
digit = scale;
if (isNaN(pctValue))
pctValue = 0;
var containerDOM = angular.element("<span style='" + "float:right;' " + cssClass + ">{{" + pctValue + "|number:" + digit + "}}</span>");
var compiled = $compile(containerDOM)(scope);
angular.element(element).replaceWith(compiled);
}
};
});
describe('directive: mi-format-number-in-grid', function() {
// initialize
var $compiler,
$rootScope;
beforeEach(angular.mock.module('cgApp'));
beforeEach(inject(function ($compile, $rootScope) {
$compile = $compile;
$rootScope = $rootScope;
}));
it("should have 2 digits after decimal", function() {
var element = $compile('<mi-format-number-in-grid numbervalue="11.97342" scale="2"></mi-format-number-in-grid>')($rootScope);
$rootScope.$digest();
expect(element.html()).toContain("11.97");
});
});
私のばかげたミスを指摘してくれてありがとう。もはや参照を取得していないが、私のテストはまだ失敗しています。 – user360
それは( "Numberは右に整列するはずです"、function(){ var要素= 'グリッド内のグリッドID = "testData" numbervalue = "11.97342" scale = "2" percentchar = "true"> var span = element.find( 'span'); var floatRight = 0グリッド内のグリッド数を指定します。 expect(span).toBeDefined(); expect(floatRight).toBe( "float:right"); }); element.attr( 'style'); 私はデバッグモードでspanが空であると見ます。 – user360
同じものを作成できますか? –