嗨有这个指令:
angular.module('xos.uiComponents.table', [])
.directive('xosTable', function(){
return {
restrict: 'E',
scope: {
data: '=',
config: '='
},
template: `
<!-- <pre>{{vm.data | json}}</pre> -->
<table ng-class="vm.classes" ng-show="vm.data.length > 0">
<thead>
<tr>
<th ng-repeat="col in vm.columns">{{col.label}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="item in vm.data">
<td ng-repeat="col in vm.columns">{{item[col.prop]}}</td>
</tr>
</tbody>
</table>
`,
bindToController: true,
controllerAs: 'vm',
controller: function(){
if(!this.config){
throw new Error('[xosTable] Please provide a configuration via the "config" attribute');
}
if(!this.config.columns){
throw new Error('[xosTable] Please provide a columns list in the configuration');
}
this.columns = this.config.columns;
this.classes = this.config.classes || 'table table-striped table-bordered';
}
}
})
我正在尝试测试它,但我无法访问isolateScope()
,这是我的测试代码:
describe('when correctly configured', function() {
let scope, element, is;
beforeEach(inject(function ($compile, $rootScope) {
scope = $rootScope.$new();
scope.config = {
columns: [
{
label: 'Label 1',
prop: 'label-1'
},
{
label: 'Label 2',
prop: 'label-2'
}
]
};
scope.data = [
{
'label-1': 'Sample 1.1',
'label-2': 'Sample 1.2'
},
{
'label-1': 'Sample 2.1',
'label-2': 'Sample 2.2'
}
]
element = angular.element('<xos-table config="config" data="data"></xos-table>');
$compile(element)(scope);
is = element.isolateScope();
scope.$digest();
}));
it('should contain 2 columns', function() {
expect(is.columns).toEqual(2);
});
});
我有很多次相同的设置,知道为什么我无法访问isolateScope
我的指令吗?
这是一个带有代码和测试的 Plunker:http ://plnkr.co/edit/JYYtck?p=preview