AngularJS service in separate file

前端 未结 3 861
故里飘歌
故里飘歌 2021-02-04 17:01

My app.js contains

var app = angular.module(\'myApp\', []).
config([\'$routeProvider\', function ($routeProvider, $http) {
    ...
}]);

Servic

3条回答
  •  太阳男子
    2021-02-04 17:24

    You need to return addNums in app.service callback.

    app.service('MyService', function () {
      addNums = function (text) {
        return text + "123";
      }
      return addNums;
    });
    

    Now, whenever you use MyService, angular will give you back the addNums function to use.

    Therefore, you should use it in your controller like so (note there isn't the addNums call):

    function adminCtrl ($scope, MyService) {
      $scope.txt = MyService("abc");
    };
    

提交回复
热议问题