加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何在Angular 1.5组件中对具有不同名称的绑定进行

发布时间:2020-12-17 17:21:52 所属栏目:安全 来源:网络整理
导读:我有以下组件: angular.module('foo') .component('searchInput',{ bindings: { text: "query" },templateUrl: 'components/searchInput/searchInput.html',controller: 'SearchInputCtrl' }); 以下通过: expect(component.text).toBe('bar'); 我必须使用
我有以下组件:

angular.module('foo')
    .component('searchInput',{
        bindings: {
            text: "<query"
        },templateUrl: 'components/searchInput/searchInput.html',controller: 'SearchInputCtrl'
    });

以下通过:

expect(component.text).toBe('bar');

我必须使用以下代码:

var component = $componentController('searchInput',{$scope: {}},{
            text: 'bar'
        }
    );

但是,我想测试绑定到’text’的值来自’query’.这不起作用:

var component = $componentController('searchInput',{
            query: 'bar'
        }
    );

解决方法

您可以通过编译组件来测试此类事物.例如

inject(function($compile,$rootScope) {
    var parentScope = $rootScope.$new();
    parentScope.myVar = 'test';

    var element = angular.element('<search-input query="myVar"></search-input>');

    var compiledElement = $compile(element)(parentScope);
    parentScope.$digest();

    var scope = compiledElement.isolateScope();

    expect(scope.$ctrl.text).toBe('test');
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读