简体   繁体   English

angularjs中的另一个函数无法识别该函数

[英]Function is not recognized by another function in angularjs

During loading of the partial Html with controller, my function named $scope.actionViewVisitors() is recognized and runs without errors. 在使用控制器加载部分Html的过程中,我的名为$scope.actionViewVisitors()函数可以识别并且可以正常运行。 But whenever I use it inside another function on the same controller, it gives me an error: TypeError: $scope.actionViewVisitors is not a function . 但是,每当我在同一控制器上的另一个函数中使用它时,都会给我一个错误: TypeError: $scope.actionViewVisitors is not a function Please see my code below: 请在下面查看我的代码:

angular.module("Visitor.controller", [])

// ============== Controllers
.controller("viewVisitorController", function ($scope, $rootScope, $http, viewVisitorService, viewAccountService, DTOptionsBuilder) {
    $scope.visitorList = null;

    $scope.viewAccountDetail = null;
    $scope.avatar = null;

    $scope.visitorDetail = null;

    $scope.visitorBtn = "Create";

    $scope.actionViewAccount = function () {
        $scope.actionViewAccount = viewAccountService.serviceViewAccount()
        .then(function (response) {
            $scope.viewAccountDetail = response.data.account;
            $scope.avatar = "../../avatars/" + response.data.account.AccountId + ".jpg";
        })
    }

    $scope.dtOptions = DTOptionsBuilder.newOptions()
        .withDisplayLength(10)
        .withOption('bLengthChange', false);

    // THIS ONE IS NOT RECOGNIZED    
    $scope.actionViewVisitors = function () {
        $scope.actionViewVisitors = viewVisitorService.serviceViewVisitors()
            .then(function (response) {
            debugger;
                $scope.visitorList = response.data.visitorList;
            });
    }

    // I DON'T GET ANY ERROR HERE
    $scope.actionViewVisitors();
    $scope.actionViewAccount();

    $scope.createVisitor = function () {
        $scope.statusMessage = null;
        if ($scope.visitorBtn == "Create") {
            $scope.createVisitor = viewVisitorService.serviceCreateVisitor($scope.visitorDetail)
                .then(function (response) {
                    if (response.data.response == '1') {
                        bootbox.alert({
                            message: "Successfully created a new visitor.",
                            size: 'small',
                            classname: 'bb-alternate-modal'
                        });
                    } else if (response.data.response == '0') {
                        bootbox.alert({
                            message: "Failed in creting visitor.",
                            size: 'small',
                            classname: 'bb-alternate-modal'
                        });
                    }
                });
            debugger;
            $scope.visitorDetail = undefined;
            // I GET THE ERROR WHEN I CALL THIS METHOD
            $scope.actionViewVisitors();
        }
    }
})

// ============== Factories
.factory("viewVisitorService", ["$http", function ($http) {
    var fac = {};

    fac.serviceViewVisitors = function () {
        return $http({
            url: '/Visitor/ViewVisitors',
            method: 'get'
        });
    }

    fac.serviceCreateVisitor = function(visitor) {
        return $http({
            url: '/Visitor/CreateVisitor',
            data: { visitor: visitor },
            method: 'post'
        });
    }

    return fac;
}])

You are overwriting the function with Promise in the following line, thus the error is correct 您在下一行中用Promise覆盖了该函数,因此错误是正确的

$scope.actionViewVisitors = function () {
    $scope.actionViewVisitors = viewVisitorService.serviceViewVisitors() 
        .then(function (response) {
           $scope.visitorList = response.data.visitorList;
        });
}

Remove $scope.actionViewVisitors = 删除$scope.actionViewVisitors =

$scope.actionViewVisitors = function () {
    viewVisitorService.serviceViewVisitors() 
        .then(function (response) {
           $scope.visitorList = response.data.visitorList;
        });
}

On the first call to the function you are changing it from a function to a Promise. 在第一次调用该函数时,您正在将其从函数更改为Promise。 Maybe you want to be returning the result instead? 也许您想返回结果呢?

$scope.actionViewVisitors = function () {
    return viewVisitorService.serviceViewVisitors()
        .then(function (response) {
        debugger;
            $scope.visitorList = response.data.visitorList;
        });
}

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

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