GVKun编程网logo

javascript – TypeError:Factory.function不是函数(js function is not defined)

1

在本文中,我们将给您介绍关于javascript–TypeError:Factory.function不是函数的详细内容,并且为您解答jsfunctionisnotdefined的相关问题,此外,我们

在本文中,我们将给您介绍关于javascript – TypeError:Factory.function不是函数的详细内容,并且为您解答js function is not defined的相关问题,此外,我们还将为您提供关于JavaScript function() 不是函数、javascript – Angular JS TypeError:$http不是函数、javascript – BackboneJs:未捕获TypeError:undefined不是函数、javascript – Function和Function.prototype之间的区别的知识。

本文目录一览:

javascript – TypeError:Factory.function不是函数(js function is not defined)

javascript – TypeError:Factory.function不是函数(js function is not defined)

我正在用web api编写我的第一个有角度的应用程序,我在调用工厂函数方面遇到了一些问题.

我有两个看起来像这样的工厂:

main.factory('Table',function ($http,$log) {
    return {
        build: function (token,cubeid) {
            return $http({
                method: 'POST',url: 'http://localhost:50051/api/structure/cube',headers: { 'Content-Type': 'application/x-www-form-urlencoded' },transformRequest: function (obj) {
                    var str = [];
                    for (var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");
                },data: { token: token,cubeId: cubeid }
            });
        }
    };
});

main.factory('Login',$log) {
    return {
        authorize: function (username,password) {
            return $http({
                method: 'POST',url: 'path/to/api/',data: { username: username,password: password }
            });
        }
    };
});

两个看起来像这样的控制器:

main.controller('loginController',['$scope','$log','$http','$location','Login',function jobListController($scope,$log,$http,$location,Login) {

    $scope.login = function () {
        Login.authorize($scope.username,$scope.password).success(function (response) {
            $location.path('/table/'+response.token);
        });
    }

}]);

main.controller('tableController','$routeParams','Table',function tableController($scope,$routeParams,Table) {
    var cube = 130;
    var token = $routeParams.token;
    $log.log($routeParams.token);
    Table.build(token,cube).success(function (response) {
        $scope.structure = response;
        $log.log(response);
    });
}]);

由于某种原因,构建函数引发错误,说“TypeError:Table.build不是函数”,而授权函数就像魅力一样.

任何人都可以向我解释为什么构建函数不起作用?

PS:我已经检查过令牌实际上是通过控制器传递的.

解决方法

您将不同的服务/工厂注入您的控制器

['$scope',Table)

应该

['$scope',Table)

JavaScript function() 不是函数

JavaScript function() 不是函数

如何解决JavaScript function() 不是函数

我有一个奇怪的错误或者我很笨,当我搜索我的错误时,我没有得到我需要的答案。

如果在文本框中按下某个键“/”,我正在尝试运行一些 javascript。

代码如下:

function ClockIn(){
    var kb_press = event.keyCode;
    if(kb_press == 47)
    {
        alert("you are clocking in");
        if(document.ClockIn.status.value === "IN"){
            alert("You Cant Clock in wile you are already Clocked in\\n Please try again!")
            document.ClockIn.tx_Barcode.value,document.ClockIn.status.value,document.ClockIn.name.value = "";
        }
    }
}
<form method="POST" name="ClockIn">
<lable>Type your BarCode <input type="text" name="tx_Barcode" id="tx_Barcode"onkeypress="ClockIn()" ></lable><br>
<lable>Is your Name? <input type="text" name="name"></lable><br>
<lable>You are currently Signed <input type="text" name="status"></lable><br>
</form>

我的结果是:ClockIn 不是函数

解决方法

这里的问题是您已将“ClockIn”表单命名为“ClockIn”表单,因此由于 HTML 与 JavaScript 交互方式的古老怪癖,ClockIn 表单会覆盖您的全局 ClockIn 函数。

也许可以重命名表单“ClockInForm”?不过,更好的是,您可能希望使用 document.getElementById("...") 来引用元素。

javascript – Angular JS TypeError:$http不是函数

javascript – Angular JS TypeError:$http不是函数

我已经阅读了所有帖子,其中人们得到这个问题,其中$http不是一个函数,并且看起来大多数情况下它是由于以错误的顺序进行注入.

我的模块定义如下所示:

angular.module("app",[]).controller("appCtrl",['$scope','$http',function ($scope,$http) {

...

    $scope.makeCall= function ($http) {
         console.log("HERE");
         $http({ method: 'GET',url: <url }).
            then(function (response) {

                console.log(response.data);
                return response.data;
            },function (response) {

        });
    };
}
])

任何建议将不胜感激.

解决方法

从makeCall函数中删除$http参数,这会消除通过控制器注入的$http依赖性的存在.基本上,当您在函数上添加它时,它被设置为undefined
$scope.makeCall= function () { //<-- removed $http dependency from here
   console.log("HERE");
   $http({ method: 'GET',url: 'url' })
      .then(function (response) {
            console.log(response.data);
            return response.data;
      },function (response) {

      }
   );
};

javascript – BackboneJs:未捕获TypeError:undefined不是函数

javascript – BackboneJs:未捕获TypeError:undefined不是函数

我遇到的问题是Stack中有很多其他帖子.但是没有人能解决这个问题所以我正在解决这个问题.

快速参考:
Uncaught TypeError: undefined is not a function rails3/backbone/js

我正在用backBoneJs编写我的第一个应用程序.这是我遵循的例子:

http://www.jamesyu.org/2011/01/27/cloudedit-a-backbone-js-tutorial-by-example/

当我执行代码时:

1 - Uncaught TypeError: Cannot call method 'extend' of undefined (Controller 1ºline)

2 - Uncaught TypeError: undefined is not a function              (app 6º line)
App.initapp.js:6
(anonymous function)/backbone/#:32
f.extend._Deferred.e.resolveWithjquery-1.6.4.min.js:2
e.extend.readyjquery-1.6.4.min.js:2
c.addEventListener.C

检查我的代码:

的index.html

<html>
    <head>
        <title></title>
        <link href="css/style.css" media="all" rel="stylesheet" type="text/css" />
    </head>

    <body>
        <h1><a href="#">Editor de Documentos</a></h1>
        <h2>Backbone + PHP by Lqdi</h2>

        <div id="notice"></div>
        <div id="app"></div>
        <script type="text/javascript" src="js/_libs/jquery-1.6.4.min.js"></script>
        <script type="text/javascript" src="js/_libs/json2.js"></script>
        <script type="text/javascript" src="js/_libs/underscore.js"></script>
        <script type="text/javascript" src="js/_libs/backbone.js"></script>
        <script type="text/javascript" src="js/_libs/jquery.dotimeout.js"></script>

        <script type="text/javascript" src="js/app.js"></script>

        <script type="text/javascript" src="js/controllers/documents.js"></script>
        <script type="text/javascript" src="js/models/document.js"></script>
        <script type="text/javascript" src="js/collections/documents.js"></script>


        <script type="text/javascript" src="js/views/edit.js"></script>
        <script type="text/javascript" src="js/views/index.js"></script>
        <script type="text/javascript" src="js/views/notice.js"></script>

        <script type="text/javascript">
            $(function() {
                App.init();
            });
        </script>
    </body>
</html>

类别:

App.Collections.Documents = Backbone.Collection.extend({
    model: Document,
    url: '/documents'
});

控制器:

App.Controllers.Documents = Backbone.Controller.extend({
    routes: {
        "documents/:id":            "edit",
        "":                         "index",
        "new":                      "newDoc"
    },

    edit: function(id) {
        var doc = new Document({ id: id });
        doc.fetch({
            success: function(model, resp) {
                new App.Views.Edit({ model: doc });
            },
            error: function() {
                new Error({ message: 'Could not find that document.' });
                window.location.hash = '#';
            }
        });
    },

    index: function() {
        var documents = new App.Collections.Documents();
        documents.fetch({
            success: function() {
                new App.Views.Index({ collection: documents });
            },
            error: function() {
                new Error({ message: "Error loading documents." });
            }
        });
    },

    newDoc: function() {
        new App.Views.Edit({ model: new Document() });
    }
});

楷模:

var Document = Backbone.Model.extend({
    url : function() {
      var base = 'documents';
      if (this.isNew()) return base;
      return base + (base.charat(base.length - 1) == '/' ? '' : '/') + this.id;
    }
});

浏览次数:

edit.js

App.Views.Edit = Backbone.View.extend({
    events: {
        "submit form": "save"
    },

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
        this.render();
    },

    save: function() {
        var self = this;
        var msg = this.model.isNew() ? 'Successfully created!' : "Saved!";

        this.model.save({ title: this.$('[name=title]').val(), body: this.$('[name=body]').val() }, {
            success: function(model, resp) {
                new App.Views.Notice({ message: msg });
                Backbone.history.saveLocation('documents/' + model.id);
            },
            error: function() {
                new App.Views.Error();
            }
        });

        return false;
    },

    render: function() {
        $(this.el).html(JST.document({ model: this.model }));
        $('#app').html(this.el);

        // use val to fill in title, for security reasons
        this.$('[name=title]').val(this.model.get('title'));

        this.delegateEvents();
    }
});

index.js

App.Views.Index = Backbone.View.extend({
    initialize: function() {
        this.render();
    },

    render: function() {
        $(this.el).html(JST.documents_collection({ collection: this.collection }));
        $('#app').html(this.el);
    }
});

notice.js

 App.Views.Notice = Backbone.View.extend({
        className: "success",
        displayLength: 5000,
        defaultMessage: '',

        initialize: function() {
            _.bindAll(this, 'render');
            this.message = this.options.message || this.defaultMessage;
            this.render();
        },

        render: function() {
            var view = this;

            $(this.el).html(this.message);
            $(this.el).hide();
            $('#notice').html(this.el);
            $(this.el).slideDown();
            $.doTimeout(this.displayLength, function() {
                $(view.el).slideUp();
                $.doTimeout(2000, function() {
                    view.remove();
                });
            });

            return this;
        }
    });

    App.Views.Error = App.Views.Notice.extend({
        className: "error",
        defaultMessage: 'Uh oh! Something went wrong. Please try again.'
    });

该应用程序:

var App = {
    Views: {},
    Controllers: {},
    Collections: {},
    init: function() {
        new App.Controllers.Documents();
        Backbone.history.start();
    }
};

解决方法:

如果您使用的是主干0.5.x,则将Backbone.Controller重命名为Backbone.Router

从文档:

Upgrading to 0.5.0+

We’ve taken the opportunity to clarify some naming with the 0.5.0 release. Controller is Now Router, and refresh is Now reset. The prevIoUs saveLocation and setLocation functions have been replaced by navigate. Backbone.sync’s method signature has changed to allow the passing of arbitrary options to jQuery.ajax. Be sure to opt-in to pushState support, if you want to use it.

javascript – Function和Function.prototype之间的区别

javascript – Function和Function.prototype之间的区别

根据 this,函数依次从Function.prototype继承Function和Function:

The global Function object has no methods or properties of its own,however,since it is a function itself it does inherit some methods and properties through the prototype chain from Function.prototype.

那么Function.prototype有什么意义呢?为什么不将其属性移动到Function并让Function.prototype未定义?函数将从Function派生而来.

对象等同样如此.

解决方法

functions inherit from Function

你松散地引用了MDN.它实际上说的是:

function objects inherit from Function.prototype

请注意,在MDN页面上,上面句子中的初始单词“function”是大写的,但仅仅因为它位于句子的开头,而不是因为它指的是JS对象Function.它指的是声明为function(){}的常规旧函数.

请记住,MDN是由凡人写的.我希望他们不使用“继承”和“继承”这两个词,更不用说“派生”了. JS严格意义上没有继承的概念.如果你使用这个术语,你最终会让自己感到困惑. JS拥有的是与对象相关的原型.访问对象上的属性时,如果找不到,则查阅原型.如果没有在那里找到,因为原型也是带有原型的对象,所以原型的原型被咨询,依此类推.

因此,上面的句子最好写成“函数对象具有原型Function.prototype”.

除了Function.prototype是Function的属性之外,JS Function对象不直接与Function.prototype相关联,并且由于Function对象本身就是一个函数,因此它本身具有Function.prototype作为其原型.无论在功能上可能存在或不存在哪些属性,或者由您挂起,都与原型链无关,并且不会被任何人“继承”.

当你执行(function(){}).call()时,首先在函数对象本身上查找call属性/方法;如果它不存在,就像通常不存在那样,那么当声明函数时,它会被内部分配的原型查找,即Function.prototype.如果没有在Function.prototype上,你还会在哪里放置调用或应用等方法?除了Function.prototype之外,您还会将原型自动分配给其他函数?

另外,请注意Function.call将正确解析为内部调用函数.为什么?不是因为那是调用的地方,或者因为那是常规函数“继承”调用的地方,而是因为,正如我之前提到的,函数本身就是一个函数,因此具有原型Function.prototype,可以找到调用在其原型链上.

What’s the point of Function.prototype then? Why not move its properties to Function and let Function.prototype be undefined? Functions would be derived from Function instead.

X.prototype是X上的一个属性,用作使用X作为构造函数创建的对象的原型.因此,要将正确的原型分配给使用Function作为构造函数创建的对象(其中包含声明为函数x(){}的函数),原型必须作为Function上的prototype属性存在.

今天关于javascript – TypeError:Factory.function不是函数js function is not defined的介绍到此结束,谢谢您的阅读,有关JavaScript function() 不是函数、javascript – Angular JS TypeError:$http不是函数、javascript – BackboneJs:未捕获TypeError:undefined不是函数、javascript – Function和Function.prototype之间的区别等更多相关知识的信息可以在本站进行查询。

本文标签:

上一篇Javascript – 在句子中反转单词(js 反转义 方法)

下一篇javascript:删除重复的斜杠和尾部斜杠(js去除重复)