久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Angular.js 使用 html2js 將指令與外部模板聯合起來

Angular.js unitest a Directive with external template using html2js - Fail to load templates(Angular.js 使用 html2js 將指令與外部模板聯合起來 - 無法加載模板)
本文介紹了Angular.js 使用 html2js 將指令與外部模板聯合起來 - 無法加載模板的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試測試使用外部模板的指令.我嘗試了以下所有解決方案,但沒有成功:

I am trying to test a Directive which uses external template. I tried all the following solutions with no luck:

  • ng-directive-testing

如何測試使用 templateUrl 和控制器?

AngularJS + Karma + Ng-html2js =>無法實例化模塊...html

我創建了一個測試指令(一個簡單的 div)并使用內聯模板"和外部模板Url"對其進行了測試.內聯解決方案有效,而外部無效:

I created a test directive (a simple div) and tested it using an inline 'template' and external 'templateUrl'. The inline solution works while the external doesn't:

   angular.module('AdUnit').directive('actionButton',function($location){
        return{
            scope:{
                actionName: '@'
            },
            restrict: 'E',
            //template: "<div ng-click='click()'>action button</div>",
            templateUrl: '/staticfiles/adunit/html/directives/actionButtonTemplate.html',
            controller: ['$scope', function($scope){
                $scope.click = function(){
                    $scope.$emit('ACTION_CLICK', $scope.actionName);
                }
            }]

        }
    });



    describe("Unit: Testing action button directive", function() {
    var elm, scope, linkFn;
    beforeEach(
        module('AdUnit')
    );

    beforeEach(module('/staticfiles/adunit/html/directives/actionButtonTemplate.html'));

    beforeEach(inject(function($rootScope, $compile) {
        elm = angular.element('<action-button action-name="post-action-0"></action-button>');
        scope = $rootScope;
        linkFn = $compile(elm);
        linkFn(scope);
        scope.$digest(); // have to digest to bring html from templateCache
        console.log('post compile',elm.html());// <== the html here still have {{}}
    }));

    it('should show a thumb',function() {

            console.log('post link',elm.html());// <== the html is bound

            expect(elm.text()).toBe("action button");

    });
});

我的 Karma 配置文件:

My Karma config file:

  module.exports = function(config) {
  config.set({

    // base path, that will be used to resolve files and exclude
    basePath: '',


    // frameworks to use
    frameworks: ['jasmine'],


    // list of files / patterns to load in the browser
    files: [
        'http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js',
        'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js',
        'http://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular-route.js',
        'http://code.angularjs.org/1.0.6/angular-mocks.js',
        '../html/*.html',
        '../html/directives/*.html',
        '../js/adUnit.js',
        '../js/controllers/*.js',
        '../js/directives/*.js',
        '../js/services/*.js',
        '../*.js',
         '../**.*.js',
         '**/*.tests.js'
    ],

    preprocessors : {
        '../html/**/*.html': ['ng-html2js']
    },

     /* ngHtml2JsPreprocessor: {
          'AdUnit': '/staticfiles/adunit/html/directives/actionButtonTemplate.html'
          *//*moduleName: '/staticfiles/adunit/html/directives/internalPlayerTemplate.html'*//*
      },*/

    // list of files to exclude
    exclude: [

    ],


    // test results reporter to use
    // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: true,


    // Start these browsers, currently available:
    // - Chrome
    // - ChromeCanary
    // - Firefox
    // - Opera (has to be installed with `npm install karma-opera-launcher`)
    // - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
    // - PhantomJS
    // - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
    browsers: ['Chrome'],


    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,


    // Continuous Integration mode
    // if true, it capture browsers, run tests and exit
    singleRun: false
  });
};

我不斷收到以下錯誤:

Failed to instantiate module /staticfiles/adunit/html/directives/actionButtonTemplate.html due to:
Error: [$injector:nomod]

任何幫助將不勝感激!

編輯:@MK Safi 的回答解決了我的問題.我錯過了以下內容:

EDIT: @MK Safi's answer solved my problem. I was missing the following:

 ngHtml2JsPreprocessor: {
     'moduleName': 'Templates',

     // Function that transforms the path to look exactly like
     // you have it in templateUrl in your Angular code
     //
     // Mine looks like this
     cacheIdFromPath: function(filepath) {
         return filepath.match(//staticfiles/adunit/html/directives/.*.html/);
     }
  },

在每次測試之前:

 beforeEach(module('Templates'));

正則表達式指向與指令的templateUrl"相同的路徑很重要,因為 html2js 將使用此路徑緩存這些模板(請參閱 html2js 了解更多詳情)

it is important for the regular expression to point to the same path as the directive's "templateUrl", since html2js will cache those templates using this path (see html2js for more details about that)

推薦答案

我在測試中正確設置了此設置,并且您的設置看起來正確,除了一些事情.

I have this setup correctly in my tests and your setup looks right, except for a few things.

對您的 Karma 配置文件進行以下更改:

Make the following changes to your Karma config file:

ngHtml2JsPreprocessor = {
  'moduleName': 'Templates',

  // Function that transforms the path to look exactly like 
  // you have it in templateUrl in your Angular code
  //
  // Mine looks like this
  cacheIdFromPath: function(filepath) {
    return filepath.match(/views/.*/)[0]
  }
}

然后在您的測試的 beforeEach 中包含您在上面的 Karma 配置中指定的 Templates 模塊:module('Templates')

Then in your test's beforeEach include the Templates module that you specified in your Karma config above: module('Templates')

beforeEach(function() {
  module('Templates')
})

這篇關于Angular.js 使用 html2js 將指令與外部模板聯合起來 - 無法加載模板的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 91免费福利在线 | 欧美国产视频一区二区 | 欧美成年视频 | 中文字幕一区二区三区在线观看 | 成人三级影院 | 国产91综合 | 国产精品美女www爽爽爽视频 | 在线免费观看视频黄 | 亚洲伊人久久综合 | 91 在线 | 99精品观看| 亚洲精品av在线 | 亚洲综合色视频在线观看 | 国产精品一区二区无线 | 精品视频一区二区三区四区 | 午夜性色a√在线视频观看9 | 精品无码久久久久久国产 | 在线免费黄色小视频 | 欧美 日韩 国产 一区 | 国产精产国品一二三产区视频 | 久操亚洲 | 国产一区二区视频在线观看 | 成人一区二区三区 | 欧美精品一区二区三区在线播放 | 亚洲精品视频在线观看免费 | 色综合国产 | 欧美在线激情 | 成人av片在线观看 | 国家一级黄色片 | 亚洲欧美一区二区三区1000 | 视频1区 | 久久99精品视频 | 国产精品入口久久 | 欧美日韩国产一区二区三区 | 99国产精品久久久久 | 中文字幕成人 | 在线看免费 | 天天操天天干天天曰 | 国产一区二 | 亚洲免费人成在线视频观看 | 嫩草国产 |