Angularjs 指令编译,带角的业力
我遇到了奇怪的问题,我被绊倒了 我的重点是测试项目中的指令列表 指令如下所示:Angularjs 指令编译,带角的业力,angularjs,jasmine,karma-runner,angular-directive,Angularjs,Jasmine,Karma Runner,Angular Directive,我遇到了奇怪的问题,我被绊倒了 我的重点是测试项目中的指令列表 指令如下所示: angular.module('lessons', []).directive('uwTextarea', ['usUmpire', 'usConfig', function (usUmpire, usConfig) { return { scope: { settings: "=" }, restrict: 'E', r
angular.module('lessons', []).directive('uwTextarea', ['usUmpire', 'usConfig', function (usUmpire, usConfig) {
return {
scope: {
settings: "="
},
restrict: 'E',
replace: true,
transclude : true,
templateUrl: 'rki/directive/uw-textarea/uw-textarea.html',
controller: function ($scope, $element) {
// blah blah
}
};
}]);
describe('uwTextarea', function() {
var scope, rscope, elem, compile;
beforeEach(module('lessons'));
beforeEach(module('directive/uw-textarea/uw-textarea.html'));
beforeEach(inject(function($rootScope, $compile) {
rscope = $rootScope;
compile = $compile;
}))
it('Directive Compiles', function() {
elem = compile('<uw-textarea></uw-textarea>')(rscope);
console.log(elem);
rscope.$digest();
});
});
测试结果如下所示:
angular.module('lessons', []).directive('uwTextarea', ['usUmpire', 'usConfig', function (usUmpire, usConfig) {
return {
scope: {
settings: "="
},
restrict: 'E',
replace: true,
transclude : true,
templateUrl: 'rki/directive/uw-textarea/uw-textarea.html',
controller: function ($scope, $element) {
// blah blah
}
};
}]);
describe('uwTextarea', function() {
var scope, rscope, elem, compile;
beforeEach(module('lessons'));
beforeEach(module('directive/uw-textarea/uw-textarea.html'));
beforeEach(inject(function($rootScope, $compile) {
rscope = $rootScope;
compile = $compile;
}))
it('Directive Compiles', function() {
elem = compile('<uw-textarea></uw-textarea>')(rscope);
console.log(elem);
rscope.$digest();
});
});
获取模板的方法是
beforeEach(inject(function($rootScope, $compile, $templateCache) {
rscope = $rootScope;
compile = $compile;
tmpl = $templateCache.get('directive/uw-textarea/uw-textarea.html');
}))
但我认为这不是正确的方式
karma.conf.js
我尝试在测试中使用示例,但我的指令并没有编译
我的代码怎么了?我知道这是一篇老文章,但最近我自己也遇到了一些问题。 这是我的指令测试的一个例子,现在可以使用了
describe('Directive', function () {
var element, scope, isolatedScope;
// Load the directive module and the html file
beforeEach(module('directives'));
beforeEach(module('path/to/directive/directive.html'));
beforeEach(inject(function ($compile, $rootScope) {
// Directive
var html = '<div somedirective></div>';
// Create the angular element
var elem = angular.element(html);
// Add stuff to scope if needed
scope = $rootScope;
scope.settings = 'bar';
//Compile with the scope
element = $compile(elem)(scope);
scope.$digest();
//Sometimes handy
isolatedScope = element.isolateScope();
it(' ... ', function () { /** Test **/ });
希望这有帮助
// Enabled plugins
plugins: [
"ng-html2js",
"karma-phantomjs-launcher",
"karma-jasmine",
"karma-ng-html2js-preprocessor"
]