简体   繁体   English

AngularJS:undefined不是工厂提供的函数

[英]AngularJS : undefined is not a function from factory

I have multiple functions inside of my factory and I cannot call my saveCharacter function from a button click using ng-click. 我的工厂内部有多个功能,无法使用ng-click通过单击按钮调用saveCharacter函数。 The getCharacters function works just fine. getCharacters函数可以正常工作。 Sorry in advance for the repost, I have gone over many different examples and cannot solve my particular issue with those. 抱歉,我无法重新发布帖子,我已经浏览了许多不同的示例,但无法解决这些问题。 I can see my functions when I log the xmlService, but i'm not sure why it won't call to it. 登录xmlService时可以看到我的函数,但是我不确定为什么它不会调用它。 I was trying to return a post to a PHP file in saveCharacter, but changed to a string return to try to test to see what my issue was. 我试图将帖子返回到saveCharacter中的PHP文件,但是更改为字符串返回以尝试测试以查看问题所在。

Thanks again for any help. 再次感谢任何帮助。

(function(){

var app = angular.module('arena', []);

app.factory('xmlService', function($http){

    var factory = {};

    factory.getCharacter = function getCharacter(){         
        return $http.get('xml/characterTemplate.xml');
    };
    factory.saveCharacter = function saveCharacter(){
        return "hello";
        //return $http.post('php/crud.php');
    };

    return factory;

});

app.controller('FighterController', ['$scope','xmlService', function($scope, xmlService){
    this.fighterList = fighterList;


    $scope.saveFighter = function saveFighter(){
        console.log(xmlService);
        xmlService.saveCharacter.success(function(data){
            console.log(data);
        });
    }

    function loadFighters(){


        xmlService.getCharacter().success(function(data){
            var x2js = new X2JS(); 
            var charactersList  = x2js.xml_str2json(data);

            for(var i = 0; i < charactersList.characters.character.length; i++)
            {
                var currentFighter = charactersList.characters.character[i];
                fighterList.push(currentFighter);

            }                
            $scope.FighterController = charactersList;
        });
    }
    loadFighters();
}]);



var fighterList = [
];

})();

Other questions I had while writing my first Angular app, what is the point of the code: 在编写第一个Angular应用程序时遇到的其他问题,代码的目的是什么:

$scope.FighterController = charactersList;

does that allow me to access the returned data on the view side? 这是否允许我在视图侧访问返回的数据? do I have to reset the scope in my saveFighter function to cause my button to work? 我必须在saveFighter函数中重置作用域以使按钮正常工作吗?

Am I setting the dependencies correctly for my app.controller, and is that dependency injection? 我是否为我的app.controller正确设置了依赖项,并且该依赖项注入是吗?

Thank you all, and any comments on how my code is setup are greatly appreciated! 谢谢大家,对于如何设置我的代码的任何评论都将不胜感激!

You haven't really explained what you did to fix this issue, so I'll explain it. 您尚未真正解释过解决此问题的方法,因此我将对其进行解释。

Here, you are trying to call xmlService.saveCharacter.success() : 在这里,您尝试调用xmlService.saveCharacter.success()

xmlService.saveCharacter.success(function(data){
        console.log(data);
});

But xmlService.saveCharacter is a function. 但是xmlService.saveCharacter是一个函数。 It has no success property; 它没有success属性; success is undefined. success是不确定的。 So this gives the error you were seeing. 因此,这给出了您所看到的错误。

You need to call xmlService.saveCharacter() : 您需要调用 xmlService.saveCharacter()

xmlService.saveCharacter().success(function(data){
        console.log(data);
});

But this is still a problem because the saveCharacter() function returns the string "hello" . 但这仍然是一个问题,因为saveCharacter()函数返回字符串"hello" This string doesn't have a success property. 该字符串没有success属性。 Yet again success is undefined , so now that causes the same error. 再一次, successundefined ,因此现在会导致相同的错误。

To fix that error, you just need to remove the return "hello"; 要解决该错误,您只需要删除return "hello"; and uncomment the code you had commented out: 并取消注释您已注释掉的代码:

factory.saveCharacter = function saveCharacter(){
    return $http.post('php/crud.php');
};

Fixing those two problems should remedy your issue. 解决这两个问题应该可以解决您的问题。

You are missing invoking a function with () change code to: 您缺少使用()将代码更改为以下内容的功能:

$scope.saveFighter = function saveFighter(){
    console.log(xmlService);

    xmlService.saveCharacter().success(function(data){
    // ----------------------^
        console.log(data);
    });
 }

$scope.FighterController = charactersList; is assigning data of characterList to scope variable and scope variable are accessible in html scope is like a bridge between controller and views. 将characterList的数据分配给作用域变量,作用域变量可以在html作用域中访问,就像控制器和视图之间的桥梁一样。

I recommend you to start reading angularjs 我建议您开始阅读angularjs

I adjusted my factory to this structure and now I can call my functions. 我将工厂调整为这种结构,现在可以调用函数了。

app.factory('xmlService', function($http){

    var factory = {
        getCharacter: function(){
            return $http.get('xml/characterTemplate.xml');
        },
        saveCharacter:function(){
            console.log('hello?');
            return $http.post('php/crud.php');
        }
    };
    return factory;

}); 

in my controller 在我的控制器中

$scope.saveFighter = function(){
        console.log(xmlService);
        xmlService.saveCharacter().success(function(data){
            console.log(data);
        });

    }

    function loadFighters(){
        xmlService.getCharacter().success(function(data){
            var x2js = new X2JS(); 
            var charactersList  = x2js.xml_str2json(data);

            for(var i = 0; i < charactersList.characters.character.length; i++)
            {
                var currentFighter = charactersList.characters.character[i];
                fighterList.push(currentFighter);

            }                
            $scope.FighterController = charactersList;
        });
    }
    loadFighters();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM