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

angularjs – 如何在茉莉花中使用“Controller as”语法使用范围

发布时间:2020-12-17 08:26:46 所属栏目:安全 来源:网络整理
导读:我正在使用jasmine进行angularJS测试。在我看来,我使用“Controller as”语法: div ng-controller="configCtrl as config" div {{ config.status }} /div/div 如何在茉莉花中使用这些“范围”变量? “控制器”是指什么? 我的测试如下所示: describe('Co
我正在使用jasmine进行angularJS测试。在我看来,我使用“Controller as”语法:
<div ng-controller="configCtrl as config">
    <div> {{ config.status }} </div>
</div>

如何在茉莉花中使用这些“范围”变量? “控制器”是指什么?
我的测试如下所示:

describe('ConfigCtrl',function(){
    var scope;

    beforeEach(angular.mock.module('busybee'));
    beforeEach(angular.mock.inject(function($rootScope){
        scope = $rootScope.$new();

        $controller('configCtrl',{$scope: scope});
    }));

    it('should have text = "any"',function(){
        expect(scope.status).toBe("any");
    });
});

调用scope.status结束,肯定有错误:

Expected undefined to be "any".

更新:Controller(从TypeScript编译的javascript)如下所示:

var ConfigCtrl = (function () {
    function ConfigCtrl($scope) {
        this.status = "any";
    }
    ConfigCtrl.$inject = ['$scope'];
    return ConfigCtrl;
})();
解决方案是在您的测试中实例化控制器时使用“控制器”语法。特别:

$ controller(‘configCtrl as config’,{$ scope:scope});

expect(scope.config.status).toBe(“any”);

以下应通过:

describe('ConfigCtrl',function(){
    var scope;

    beforeEach(angular.mock.module('busybee'));
    beforeEach(angular.mock.inject(function($controller,$rootScope){
        scope = $rootScope.$new();

        $controller('configCtrl as config',function(){
        expect(scope.config.status).toBe("any");
    });
});

(编辑:李大同)

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

    推荐文章
      热点阅读