1

我想測試我的控制器。我是能夠進行簡單的控制器角度js和能夠在線測試控制器。這裏是我的代碼「homecntrl」不是一個功能,在angularjs得到未定義

http://plnkr.co/edit/xzvhXHPoUdulOM9clOkQ?p=preview

控制器代碼

(function(){ 
    'use strict'; 
    angular.module('app.home').controller('homeCntrl',homeCntrl); 

    function homeCntrl(){ 
    var home=this; 
    home.clickbtn=function(){ 

     home.message='test'; 
     alert(home.message) 
    } 
    } 
})(); 

但是當測試在我PC它無法運行,並收到此錯誤

**Argument 'homecntrl' is not a function, got undefined 
     http://errors.angularjs.org/1.4.8/ng/areq?p0=homecntrl&p1=not%20a%20function%2C%20got%20undefined 
      at /Users/naveenkumar/Documents/ionic_work/SimpleDemo/bower_components/angular/angular.js:68:12** 

這裏是我測試的代碼

(function(){ 

    'use strict' 
    describe('http controller test', function() { 

     var $rootScope, 
      $scope, 
      controller, 
      $q, 
      $httpBackend; 

     beforeEach(function() { 
      module('app'); 

      inject(function($injector) { 
       $rootScope = $injector.get('$rootScope'); 
       $scope = $rootScope.$new(); 
       controller = $injector.get('$controller')('homecntrl', { 
        $scope: $scope 
       }) 


      }) 
     }) 




     describe('Init value', function() { 
      it('check name value', function() { 
       expect(controller.message).toBeUndefined(); 
      }) 

     }) 

     it('it should be true', function() { 
      expect(true).toBeTruthy(); 

     }) 
    }) 


})() 

我能夠在線,但沒能測試PC上

這裏測試我的電腦代碼 https://dl.dropbox.com/s/no901z41bza7osm/SimpleDemo.zip?dl=0

請下載並進入項目目錄並寫入npm install

回答

0

應當更新控制器名稱homeCntrlhomecntrl

(function(){ 
    'use strict'; 
    angular.module('app.home').controller('homecntrl',homeCntrl); 

    function homecntrl(){ 
    var home=this; 
    home.clickbtn=function(){ 

     home.message='test'; 
     alert(home.message) 
    } 
    } 
})(); 

這將是工作。

+0

仍然無法正常工作..請下載我的代碼,然後檢查 – user944513

+0

請再次檢查不起作用 – user944513

相關問題