如何使用查看ng-model中的不同参数再次运行控制器功能

时间:2021-12-14 00:09:54

I have a controller that works fine on initial load. It calls user [0] data and everything processes fine. When I change dropdown, I want to call the same function, but I cannot get the entire controller to reload. It starts from the function call and leaves undefined in places where it pulls correct information (linkToken, etc) on initial load. Is there a way I can get it to reload all data from the controller instead of just from the function?

我有一个控制器在初始加载时工作正常。它调用用户[0]数据,一切正常。当我更改下拉列表时,我想调用相同的函数,但我无法重新加载整个控制器。它从函数调用开始,并在初始加载时提取正确信息(linkToken等)的地方保留undefined。有没有办法让我从控制器重新加载所有数据,而不仅仅是从函数中重新加载?

After calling the testchange() from the view to pass in the new data, I get : TypeError: Cannot read property 'locations' of undefined at h.$scope.updateLocations (refillController.js:261) at refillController.js:73

从视图中调用testchange()以传入新数据后,我得到:TypeError:无法在refillController.js中读取h. $ scope.updateLocations(refillController.js:261)中未定义的属性'locations':73

But, when I call the original getRefills() that is ran on the initial page load I get the same error. How can I define these so the load after the onchange(0)?

但是,当我调用在初始页面加载时运行的原始getRefills()时,我得到相同的错误。如何在onchange(0)之后定义这些负载呢?

angular.module('FinalApp.Controllers').controller('refillController', function ($rootScope, $scope, $location, $modal, userService, dependentsService, accountService, sharedCollections, configurationService, refillsService) {

$scope.user = userService.GetUserInformation();
$scope.userInfoArr = [];
//$scope.tests.push({'Name':$scope.user.FirstName, 'LinkToken':$scope.user.LinkToken});
$scope.userInfoArr = $scope.user.Dependants.map(function(item){return {'Name':item.FirstName, 'LinkToken':item.LinkToken}});
$scope.userInfoArr.splice(0, 0, {'Name': $scope.user.FirstName, 'LinkToken': $scope.user.LinkToken});
console.log($scope.userInfoArr);
$scope.finUserInfoArr = $scope.userInfoArr[0].LinkToken;
$scope.billingInfo = null;
$rootScope.showNavbar = true;

$scope.selectedMethod = null;
$scope.location = null;
$scope.payment = null;

$scope.refills = [];
$scope.deliverTypes = [];
$scope.locations = [];
$scope.payments = [];
$scope.allSelected = false;
$scope.loadingBillingInfo = false;
$scope.isMailOrder = false;
    //Detect Mobile Switch Refill List To Grid
    if(window.innerWidth <= 800) {
        $scope.view = "Grid";
    } else {
        $scope.view = "List";
    }

$scope.changeViewToList = function () {
    $scope.view = "List";
};

$scope.changeViewToGrid = function () {
    $scope.view = "Grid";
};

$scope.testchange = function(selectedTest) {
    $scope.getRefills(selectedTest);


    console.log(selectedTest);

};

$scope.getRefills = function (linkToken) {
    $scope.allSelected = false;
    $scope.loading = true;

    refillsService.GetRefills(
        linkToken,
        $scope.selectedMethod,
        $scope.location,
        $scope.payment
    ).then(function (data) {
        $scope.refills = [];
        _.each(data.Prescriptions, function (item) {
            fillRefills(item);
        });
        fillDeliverTypes(data.DeliveryTypes);
        if (!$scope.selectedMethod)
            $scope.selectedMethod = data.DeliveryTypeId;

        if (!$scope.location)
            $scope.location = data.PickupLocationId;

        if (!$scope.payment)
            $scope.payment = data.PaymentTypeId;

        $scope.loading = false;

        $scope.updateLocations();
    })["catch"](function (error) {
        console.log(error);
        $scope.loading = false;
        alertify.alert(configurationService.ErrorMessage("getting your refills", error.Message));
    });
};



var fillRefills = function (item) {
   //TODO-CallDoc temp fix
    if (item.RefillClass == "CALL_DOCTOR") {
        item.NextRefillDate = '1900-01-01T00:00:00'
    }
    var parsedDate = checkDate(moment(item.NextRefillDate).format('L'));
    var lastrefill = checkDate(moment(item.LastDispenseDate).format('L'));
    var expireDate = checkDate(moment(item.ExpirationDate).format('L'));
    var status = (item.RefillStatus.indexOf("After") == -1) ? item.RefillStatus : "Refill " + item.RefillStatus;
    $scope.refills.push({
        selected: false,
        rx: item.ScriptNo,
        name: item.DrugName,
        dose: item.UnitsPerDose,
        dir: item.Instructions,
        nextfill: parsedDate,
        lastfill: lastrefill,
        refillsLeft: item.NumRefillsLeft,
        status: status,
        msg: item.RefillMessage,
        canSelect: item.IsRefillable,
        refillClass: item.RefillClass,
        lastDispenseQty: item.LastDispenseQty,
        DaysSupply: item.DaysSupply,
        expireDate: expireDate,
        copayAmt: item.CopayAmt,
        drFirstName: item.DoctorFirstName,
        drLastName: item.DoctorLastName,
        writtenQty: item.WrittenQty
    });
};

var checkDate = function (date) {
    if (date == "01/01/1900") return "N/A";

    if (date == "Invalid Date") return "";
    return date;
};

var fillDeliverTypes = function (deliverTypes) {
    $scope.deliverTypes = [];
    _.each(deliverTypes, function (item) {
        $scope.deliverTypes.push({
            id: item.DeliveryTypeId,
            name: item.DeliveryTypeName,
            locations: item.PickupLocations,
            payments: item.PaymentTypes
        });
    });
};


var getBillingInfo = function () {
    $scope.loadingBillingInfo = true;
    accountService.GetCreditCardInfo().then(function (data) {
        $scope.billingInfo = data;
        $scope.loadingBillingInfo = false;
    })["catch"](function (error) {
        $scope.loadingBillingInfo = false;
        alertify.alert(configurationService.ErrorMessage("getting account information", error.Message));
    });
};

var getAccountInfo = function () {
    accountService.GetAccountInfo().then(function (data) {
        if (data.StatusCode == "SUCCESS") {
            $scope.user = data;
            userService.SaveUserInformation(data);
            if ($scope.user.IsLinked) {
                $rootScope.enableMyRefills = true;
                $rootScope.enableMyReports = true;
                window.location.hash = "#/refills";
            } else {
                $rootScope.enableMyRefills = false;
                $rootScope.enableMyReports = true;
            }
        } else {
            alertify.alert(configurationService.ErrorMessage("getting account information", data.StatusMessage));
        }
    })["catch"](function (error) {
        alertify.alert(configurationService.ErrorMessage("getting account information", error.Message));
    });
};


var openModal = function (viewUrl, controllerUrl, size, payload) {
    var modalInstance = $modal.open({
        templateUrl: viewUrl,
        controller: controllerUrl,
        size: size,
        resolve: {
            data: function () {
                return payload;
            }
        }
    });
    return modalInstance;
};

$scope.toggleRxSelection = function(rx) {
    if (rx.canSelect) {
        rx.selected = !rx.selected;
        $scope.evaluateAllSelected();
    }
};

$scope.selectAll = function (data) {
//        $scope.allSelected = allSelected;
    _.each($scope.refills, function (x) {
        if (x.canSelect) x.selected = data;
    });
};

$scope.evaluateAllSelected = function () {
    var count = _.countBy(_.where($scope.refills, {canSelect:true}), function(refill) {
       return refill.selected ? 'selected' : 'not';
    });

    $scope.allSelected = (count.not === undefined);
};

$scope.openEditCreditCardInfo = function () {
    var payload = ($scope.billingInfo != null && $scope.billingInfo != undefined)
    && $scope.billingInfo.CardNumber != "" ? $scope.billingInfo : {};

    if (payload != {}) {
        payload.ExpMonth = {id: parseInt(payload.ExpMonth)};
        payload.ExpYear = {id: parseInt(payload.ExpYear)};
    }

    openModal('app/views/editAccount/billingInformation.html', "billingInformationController", "xlg", payload).result.then(function () {
        getAccountInfo();
        getBillingInfo();
    }, function () {
        getBillingInfo();
    });
};

$scope.openConfirmOrder = function () {
    var refillsSelected = _.where($scope.refills, {selected: true});
    var location = _.findWhere($scope.locations, {PickupLocationId: $scope.location});
    var payment = _.findWhere($scope.payments, {PaymentTypeId: $scope.payment});
    var deliver = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod});
    if (refillsSelected.length == 0) {
        alertify.error("You need to select at least one refill");
        return;
    }

    if (deliver.id == 10001 && !$scope.user.IsCreditCardOnFile) {
        alertify.error("Need credit card on file for mail order");
        return;
    }

    sharedCollections.setRefills(refillsSelected);
    sharedCollections.setLocation(location);
    sharedCollections.setPayment(payment);
    sharedCollections.setDeliver(deliver);
    openModal('app/views/refills/confirmOrder.html', "confirmOrderController", "xlg").result.then(function () {
        $scope.billingInfo = accountService.GetCreditCardInfo();
        $scope.getRefills();
    }, function () {
        //$scope.billingInfo = accountService.GetCreditCardInfo();
        //getRefills();
    });

};

$scope.showRefill = function (rx) {
    var data = {rx: rx, refills: $scope.refills};
    openModal('app/views/refills/showRefills.html', "refillsCarrousel", "xlg", data).result.then(function () {
        $scope.evaluateAllSelected();
    }, function () {
        $scope.evaluateAllSelected();
    });
};


$scope.updateLocations = function () {
    $scope.locations = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod}).locations;
    $scope.payments = _.findWhere($scope.deliverTypes, {id: $scope.selectedMethod}).payments;
    setLocationAndPayment();

};

var setLocationAndPayment = function () {
    if ($scope.locations.length == 1) {
        $scope.location = $scope.locations[0].PickupLocationId;
    }

    if ($scope.payments.length == 1) {
        $scope.payment = $scope.payments[0].PaymentTypeId;
    }
    //check for mail order
    ($scope.selectedMethod == 10001 && !$scope.payment) ? $scope.isMailOrder = true : $scope.isMailOrder = false;
};


$scope.getRefills($scope.finUserInfoArr);
getBillingInfo();

});

1 个解决方案

#1


0  

Check if your refillsService returns correct data, it could be that $scope.refills remains empty array.

检查你的refillsService是否返回正确的数据,可能是$ scope.refills仍为空数组。

#1


0  

Check if your refillsService returns correct data, it could be that $scope.refills remains empty array.

检查你的refillsService是否返回正确的数据,可能是$ scope.refills仍为空数组。