控制器中的AngularJS工厂出错

时间:2017-02-13 12:47:33

标签: javascript angularjs

我使用以下代码来使用工厂,我收到此错误 -

  

错误:[$ injector:undef] http://errors.angularjs.org/1.6.1/ $ injector / undef?p0 = ShowService

这是控制器的代码 -

'use strict';

app.controller("LoginController", function (ShowService) {

var model = this;

//function call when user submit the login form
model.login = function (useremail, password) {
    var data = {useremail:useremail, password,password};
    var url = "/php/user-login.php";
    ShowService.UserLogin(url,data);
}
});

这是工厂的代码 -

'use strict';

app.factory('ShowService', dataService);

function dataService($http) {
var data = {
    "UserLogin": UserLogin,
}

function UserLogin(postUrl, data) {
    return $http({
        method: "POST",
        url: post_url,
        data: data
    })
}
}

4 个答案:

答案 0 :(得分:1)

您错过了return的{​​{1}}声明。您的factory var上还有一个拼写错误,带有额外的逗号。

你应该这样做:

data

答案 1 :(得分:0)

似乎您在创建data时忘记返回ShowService变量。

并注意为控制器和服务注入必要的依赖项。斯塔克的回答可能对此有所帮助。

'use strict';

function dataService($http) { 
    function UserLogin(postUrl, data) {
        return $http({
            method: "POST",
            url: post_url,
            data: data
        });
   }

    var data = { "UserLogin": UserLogin};
    return data;
}

dataService.$inject = ["$http"];

app.factory('ShowService', dataService);

答案 2 :(得分:0)

请更改此行               var data = {useremail:useremail,password,password}; as var data = {useremail:useremail,password:password};

答案 3 :(得分:-2)

在控制器中使用this而不是app.controller("LoginController", ['$scope', 'ShowService', function ($scope, ShowService) { //function call when user submit the login form $scope.login = function (useremail, password) { var data = {useremail:useremail, password,password}; var url = "/php/user-login.php"; ShowService.UserLogin(url,data); } }]);

        wcx = new WNDCLASSEX();
        wcx.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(WNDCLASSEX)));          // size of structure 
        wcx.style = ClassStyles.HorizontalRedraw | ClassStyles.VerticalRedraw; // redraw if size changes 
        wcx.lpfnWndProc = ProcessWndProc;     // points to window procedure 
        wcx.cbClsExtra = 0;                // no extra class memory 
        wcx.cbWndExtra = 0;                // no extra window memory 
        wcx.hInstance = hinstance;         // handle to instance 
        wcx.hIcon = IntPtr.Zero;              // predefined app. icon 
        wcx.hCursor = IntPtr.Zero;                    // predefined arrow 
        wcx.hbrBackground = IntPtr.Zero;                  // white background brush 
        wcx.lpszMenuName = "MainMenu";    // name of menu resource 
        wcx.lpszClassName = "EDIT";  // name of window class 
        wcx.hIconSm = IntPtr.Zero;

        // Register the window class. 
        return RegisterClassEx(ref wcx)!=0;