Javascript 为什么不是';t$digest在我的单元测试中更新我的范围
我有以下代码。出于某种原因,测试1失败了。谁能告诉我为什么Javascript 为什么不是';t$digest在我的单元测试中更新我的范围,javascript,angularjs,jasmine,angularjs-directive,Javascript,Angularjs,Jasmine,Angularjs Directive,我有以下代码。出于某种原因,测试1失败了。谁能告诉我为什么 angular. module('myModule', []). directive('myDirective', function () { return { restrict: 'E', scope: { myAttr: '=' }, link: function (scop
angular.
module('myModule', []).
directive('myDirective', function () {
return {
restrict: 'E',
scope: {
myAttr: '='
},
link: function (scope, element, attrs) {
element.text(scope.myAttr);
}
}
});
describe('test', function () {
var compile, rootScope;
beforeEach(module('myModule'));
beforeEach(inject(function ($compile, $rootScope) {
compile = $compile;
rootScope = $rootScope;
}));
describe('test 1', function () {
it('test', function () {
scope = rootScope.$new();
// scope.myVar = "test";
element = compile('<my-directive my-attr="myVar" />')(scope);
scope.$digest();
scope.myVar = "test";
scope.$digest();
expect(element.text()).toBe("test");
});
});
describe('test 2', function () {
it('test', function () {
scope = rootScope.$new();
scope.myVar = "test";
element = compile('<my-directive my-attr="myVar" />')(scope);
scope.$digest();
scope.myVar = "test";
scope.$digest();
expect(element.text()).toBe("test");
});
});
});
angular。
模块('myModule',[])。
指令('myDirective',函数(){
返回{
限制:'E',
范围:{
myAttr:“=”
},
链接:函数(范围、元素、属性){
element.text(scope.myAttr);
}
}
});
描述(‘测试’、功能(){
var编译,rootScope;
在每个(模块(“myModule”)之前;
beforeach(注入函数($compile,$rootScope){
compile=$compile;
rootScope=$rootScope;
}));
描述('测试1',功能(){
它('test',function(){
scope=rootScope.$new();
//scope.myVar=“测试”;
元素=编译(“”)(范围);
范围。$digest();
scope.myVar=“测试”;
范围。$digest();
expect(element.text()).toBe(“test”);
});
});
描述('测试2',功能(){
它('test',function(){
scope=rootScope.$new();
scope.myVar=“测试”;
元素=编译(“”)(范围);
范围。$digest();
scope.myVar=“测试”;
范围。$digest();
expect(element.text()).toBe(“test”);
});
});
});
元素.text(scope.myAttr)代码>需要包装在手表中,如下所示:
angular.
module('myModule', []).
directive('myDirective', function () {
return {
restrict: 'E',
scope: {
myAttr: '='
},
link: function (scope, element, attrs) {
scope.$watch('myAttr', function () {
element.text(scope.myAttr);
});
}
}
});