AngularJS Error: $injector:unpr Unknown Provider

后端 未结 13 917
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 08:10

I\'m trying to build my own service by following the example in the documentation for the factory methodology. I think I\'ve done something wrong however because I continue

13条回答
  •  一向
    一向 (楼主)
    2020-12-13 08:52

    @ user2310334 I just tried this, a VERY basic example:

    HTML file

    
    
    
    
        
        
        
    
    
    
        

    The javascript file:

    var myApp= angular.module('app', ['ngRoute']);
    
    myApp.factory('mailService' , function () {
    return {
        getData : function(){
          var employees = [{name: 'John Doe', id: '1'},
            {name: 'Mary Homes', id: '2'},
            {name: 'Chris Karl', id: '3'}
          ];
    
          return employees;
        }
      };
    
    });
    
    
    myApp.controller('MailDetailCtrl',['$scope', 'mailService', function($scope, mailService) {
      alert(mailService.getData()[0].name);
    }]);
    

    And it works. Try it.

提交回复
热议问题