在这个小提琴里http://jsfiddle.net/FlavorScape/fp1kktt9/我尝试在控制器上设置属性,而不是直接在 $scope 上设置属性。在模板(在生产中)中,我们只执行 myAliasCtrl.somePropertyList ,并且 ng-repeat 起作用。
然而,这在测试中不起作用。我不知道如何获取/分配控制器上的属性。
UPDATE:我的测试环境中一定遇到了一些奇怪的局部问题,因为我确实让 ng-repeat 正常工作。请注意如何有两个子元素,即使该属性位于别名控制器上。http://jsfiddle.net/FlavorScape/2adn983y/2/
然而,我的问题仍然是,我如何获得对已编译控制器的引用来创建间谍?(或者只是获取对别名控制器的引用)?
这是来源:
angular.module('myApp', [])
.directive('myTestDirective', function () {
return {
restrict: 'E',
scope: {
myBinding: '='
},
replace: true,
template: '<div ng-if="isRendered">TEST<div ng-repeat="foo in myCtrl.fooList">{{foo}}</div></div>',
controller: 'myTestController',
controllerAs: 'myCtrl'
};
})
.controller('myTestController', function($scope) {
$scope.isRendered = true;
// if i reference this, ng-repeat works
$scope.fooList = ["boob","cat", "Tesla"];
});
describe('myTest directive:', function () {
var scope, compile, validHTML;
validHTML = '<div><my-test-directive my-binding="isRendered"></my-test-directive></div>'; //i
beforeEach(module('myApp'));
beforeEach(inject(function($compile, $rootScope){
scope = $rootScope.$new();
scope.isRendered = true;
compile = $compile;
}));
function create() {
var elem, compiledElem;
elem = angular.element(validHTML);
compiledElem = compile(elem)(scope);
scope.$digest();
return compiledElem;
}
it('should have a scope on root element', function () {
var el = create();
// how to get the controller???
el.scope().myCtrl.fooList = ["monkey","apple","Dishwasher"];
// notice it just has <!-- ng-repeat
console.log( el );
expect(el.text()).toContain('TEST');
});
});