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



我有一个控制器在初始加载时工作良好。它调用user[0]数据,一切都处理得很好。当我改变dropdown时,我想调用相同的函数,但我无法让整个控制器重新加载。它从函数调用开始,并在初始加载时提取正确信息(linkToken等)的地方留下未定义。是否有一种方法可以让它从控制器重新加载所有数据,而不仅仅是从函数?

从视图调用testchange()传递新数据后,我得到:类型错误:无法读取未定义的属性"locations"在h。美元范围。updateLocations (refillController.js: 261)在refillController.js: 73

但是,当我调用在初始页面加载时运行的原始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();

});

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

最新更新