【问题标题】:angular's element.isolateScope() returns undefinedangular 的 element.isolateScope() 返回 undefined
【发布时间】:2015-02-10 11:53:34
【问题描述】:

我正在为this directive 编写一个测试,并且我相信我做得正确,但是当我尝试访问isolateScope 时,我得到了未定义。

指令:

describe('directive: deposit-note', function() {
  var expect = chai.expect;

  var element,
      scope,
      $rootScope,
      $compile,
      $httpBackend;

  beforeEach(module('directives.depositnote'));

  beforeEach(inject(function (_$compile_, _$rootScope_) {
      $compile = _$compile_;
      $rootScope = _$rootScope_;
  }));

  beforeEach(inject(function($injector) {
    $httpBackend = $injector.get('$httpBackend');
  }));

  describe('with a filled-out transaction object', function() {

    beforeEach(inject(function($injector) {
      $httpBackend.whenGET('/templates/depositnote.tpl.html').respond(200, '');
      scope = $rootScope.$new();

      element = '<deposit-note checknumber="transaction.checkNumber" checktype="transaction.checkType.value" depositnote="transaction.depositNote" />';

      scope.transaction = {
        "checkNumber": "1234",
        "checkType": "CASHIERS CHK",
        "depositNote": "In Person"
      };

      element = $compile(element)(scope);
      scope.$digest();
    }));

    it("should calculate the correct remaining characters", function() {
      var isolated = element.isolateScope();
      expect(isolated.totalRemaining).toBe(17);
    });  
  });
});

当我逐步执行此操作时,我看到元素已编译,并且 chrome 的 devtools 显示 element.isolateScope() 确实是元素上的方法,但它返回未定义。

我错过了什么?

【问题讨论】:

  • 标题中的函数名拼写错误:isolatedScope() 应该是isolateScope()。只是对像我这样的复制粘贴者的警告:-) >(刚刚修复)

标签: angularjs unit-testing chai


【解决方案1】:

我的 karma.config 文件的基本 URL 不正确,无法访问模板缓存中的模板(我使用 html2js)。所以本质上模板是不可用的,所以指令永远不会编译,这就是为什么永远不会创建隔离范围的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    相关资源
    最近更新 更多