[英]Why isn't $digest updating my scope in my unit test
I have the following code. 我有以下代码。 For some reason test 1 is failing.
由于某种原因,测试1失败。 Can anyone tell me why?
谁能告诉我为什么?
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");
});
});
});
element.text(scope.myAttr);
needs to be wrapped in a watch, like so: 需要包裹在手表中,如下所示:
angular.
module('myModule', []).
directive('myDirective', function () {
return {
restrict: 'E',
scope: {
myAttr: '='
},
link: function (scope, element, attrs) {
scope.$watch('myAttr', function () {
element.text(scope.myAttr);
});
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.