错误:[$ injector:unpr]未知提供者:musicServiceProvider< - musicService< - MusicController

时间:2017-06-08 15:33:43

标签: angularjs

我尝试添加服务以从API获取数据。 但它给出了一个错误

  

错误:[$ injector:unpr]未知提供者:musicServiceProvider< -   musicService< - MusicController"

控制器

(function (app) {
var MusicListController = function ($scope, musicService) {
    $scope.message = " Jagadish K M";
    musicService.getAll().then(function (response) {
        $scope.musics = response.data;
        console.log($scope.musics);
    });
  };
app.controller("MusicController", MusicListController);
    }(angular.module("theMusic")));

服务 (function(app){

var musicService = function ($http, musicApiUrl) {
    var getAll = function () {
        return $http.get(musicApiUrl);
    };
    var get = function () {
        return $http.get(musicApiUrl);
    }
    var getById = function (id) {
        return $http.get(musicApiUrl + id);
    };

    var update = function (music) {
        return $http.put(musicApiUrl + music.id, music);
    };

    var create = function (music) {
        return $http.post(musicApiUrl, music);
    };

    var destroy = function (id) {
        return $http.delete(musicApiUrl + id);
    };

    return {
        getAll: getAll,
        getById: getById,
        update: update,
        create: create,
    };
};
app.factory("musicService", musicService);
}(angular.module("theMusic")));

我是棱角分明的新手。提前致谢

2 个答案:

答案 0 :(得分:2)

你没有注射你的服务。

angular.module("theMusic", ["musicService"])

您应该查看 AngularJS Dependency Injection 文档。

答案 1 :(得分:0)

试试这个

angular.module("theMusic", ["musicService"])