Angular $ scope属性undefined

时间:2016-06-05 00:16:27

标签: angularjs angularjs-directive angularjs-scope jasmine

我正在尝试将$scope.sang属性设置为指令中的SangService实例。问题是,它似乎没有设置。

设置

唱-player.directive.js:

angular.module('sang').directive('sangPlayer', [
  'SangService',
  function(SangService) {
    return {
      restrict: 'EA',
      scope: true,
      link: function(scope, _elem, attr) {
        window.console.log('link');
        scope.sang = SangService.$new();
      }
    };
  }]);

唱-player.directive.spec.js:

describe('The Sang Directive', function() {
  var compile, scope, element, sang;

  beforeEach(function() {
    sang = jasmine.createSpyObj('sang',
      ['play', 'pause', 'playPause', 'previous', 'next', 'seek']);

    module(function($provide) {
      $provide.value('Sang', sang);
    });

    inject(function($compile, $rootScope) {
      compile = $compile;
      scope = $rootScope.$new();
      element = getCompiledElement();
    });
  });

  function getCompiledElement() {
    var element = angular.element('<div sang-player></div>');
    var compiledElement = compile(element)(scope);
    scope.$digest();
    return compiledElement;
  }

  it('creates a scope.sang object', function() {
    expect(scope.sang).toBeDefined();
    expect(typeof scope.sang).toBe('object');
  });
});

Jasmine规范运行失败的输出:

 The Sang Directive
 X creates a scope.sang object
   Expected undefined to be defined. (1)
   Expected 'undefined' to be 'object'. (2)

不确定为什么link回调没有输出。我希望在compile(element)(scope)scope.$digest()内调用此内容。

The Deets

  • Angular 1.4.1
  • Jasmine 2.4.1
  • grunt-contrib-jasmine 0.9.1
  • 如果你想修补整个项目回购,here

0 个答案:

没有答案