本文介绍了服务注入到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的角。我有一个简单的网页,但codeS的控制器不工作。看来我不叫或控制器正确注入服务ListLogsFactory。请帮忙。谢谢你。

我的codeS包括:模块,服务和控制器,所有的声明/定义如下:

VAR对myApp = angular.module(ListLogsModule,[]);
    myApp.factory('ListLogsFactory',函数($ HTTP){
            变种thisPageNumber = 1;
            变种thisPageSize = 10;
            VAR的baseUrl ='../Api/LogApi/GetLogsByPage';            变种项= {};            $ HTTP({
                方法:GET,
                网址:的baseUrl,
                数据:$ .PARAM({PAGENUMBER:thisPageNumber,每页:thisPageSize})
            })
            .success(功能(数据,状态,头,配置){
                项目=数据;
                的console.log(项目);
            })
            .error(功能(数据,状态,头,配置){
                警报('错误:'+状态);
            });            函数的getData(){
                返回的物品;
            }
    });    //错误是出现在Firefox和控制器code发生:
    myApp.controllers.ListLogsController =功能($范围,ListLogsFactory){
       $ scope.logs = ListLogsFactory.getData(); //注:此行会抛出错误与类似ListLogsFactory运行不确定
   }


解决方案

当您使用工厂你必须返回的东西。你只是定义了一堆方法有,但他们没有提供给任何人。

这也是很好用不同的命名约定。例如,而不是LogsController的,使用LogsCtrl。 AngularJS追加控制器内部和你可能最终,在异国情调的情况下,处理像LogsControllerController的名字。

使用工厂并返回服务的简化

I am new to Angular. I have a simple web page, but the codes for controller are not working. It seems I do not call or inject service "ListLogsFactory" in the controller properly. Please help. Thank you.

My codes include a module, service, and controller that all declared/defined as follows:

    var myApp = angular.module("ListLogsModule", []);


    myApp.factory('ListLogsFactory', function ($http) {
            var thisPageNumber = 1;
            var thisPageSize = 10;
            var baseUrl = '../Api/LogApi/GetLogsByPage';

            var items = {};

            $http({
                method: 'GET',
                url: baseUrl,
                data: $.param({ pageNumber: thisPageNumber, pageSize: thisPageSize })
            })
            .success(function (data, status, headers, config) {
                items = data;
                console.log(items);
            })
            .error(function (data, status, headers, config) {
                alert('error: ' + status);
            });

            function getData() {
                return items;
            }
    });

    // The error is seen in FireFox and happens in the controller code:
    myApp.controllers.ListLogsController = function ($scope, ListLogsFactory) {
       $scope.logs = ListLogsFactory.getData(); // NOTE: this line throws error on running with something like "ListLogsFactory" is undefined
   }
解决方案

When you use factory you have to return something. You're just defining a bunch of methods there but they aren't available to anybody.

It's also good to use a different naming convention. For example, instead of LogsController, use LogsCtrl. AngularJS appends "Controller" internally and you might end up, in exotic situations, handling names like "LogsControllerController".

A simplified approach of using factory and returning the service:

var ListLogsModule = angular.module("myApp", []);

ListLogsModule.factory('ListLogsSrv', function ($http) {
    // first define the service (you're using a factory)
    var getData = function() {
        return "hey";//items;
    };

    // then return it.
    // offer a public method "getData" that uses your internal getData()
    return {
        getData : getData
    }
});

ListLogsModule.controller("ListLogsCtrl", function ($scope, ListLogsSrv) {
    $scope.w = "world";
    $scope.logs = ListLogsSrv.getData();
});

You also have an $http request in the factory. That means that you'll trigger the async request when you instantiate the service (when it is used for the first time), so nobody will wait for it to finish and you'll be getting undefined.If you are using this service in a controller, you will probably need to resolve a promise.

An example of using a promise:

var promise = $q.defer();
var thisPageNumber = 1;
...
var baseUrl = '../Api/LogApi/GetLogsByPage';
...
promise = $http.get(...

Now you can use this promise in the controller, for example, or in the methods of your service.

I answered a related question two days ago Angular Service Defination: server or factory

这篇关于服务注入到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:22
查看更多