Angular JS在$ uibModal.open()中返回未定义的数据

时间:2018-05-07 17:58:42

标签: javascript angularjs

以下代码返回以角度js返回未定义的项目。

App.js代码

console.log('single page application script is working');
var myApp = angular.module("demoApp", ["ngRoute", 'ui.bootstrap','chart.js']);
myApp.controller('CtrlOne', CtrlOne);
myApp.controller('CtrlTwo', CtrlTwo);
myApp.controller('LocationController', LocationController);
myApp.controller('GridController', GridController);
myApp.controller('PaginationDemoCtrl', PaginationDemoCtrl);
myApp.controller('ViewProductController', ViewProductController);

var configFunction = function ($routeProvider, $httpProvider) {
    $routeProvider.
        when('/grid', {
            templateUrl: 'SPA/Views/Grid.html',
            controller:GridController

        })
        .otherwise({
            redirectTo: function () {
                return '/grid';
            }

        });
}
configFunction.$inject = ['$routeProvider', '$httpProvider'];
myApp.config(configFunction);

GridController.js

var GridController = function ($scope, $uibModal) {
    $scope.data = {
        lowStockData: {
            totalItems: 726,
            currentPage: 1,
            itemsPerPage: 10,
            data:[]

        }
    };
    function getData() {
        $scope.data.lowStockData.data = []; //initialize the data inside the lowStockData to empty
        for (var i = 0; i < $scope.data.lowStockData.itemsPerPage; i++) {
            var currentLocation = $scope.selectedLocation.Location;

            var rndNum = ($scope.data.lowStockData.currentPage * 10) + i;

            $scope.data.lowStockData.data.push({
                SKU: "SKU" + rndNum,
                ProductTitle: "Product Title" + rndNum,
                OnOrder: rndNum * 2,
                Due: rndNum - 1,
                StockLe: rndNum,
                Location:currentLocation

            });
        }
    }
    getData();
    $scope.pageChanged = function () {
        getData();

    }
    $scope.$watch('selectedLocation', function () {
        console.log($scope.selectedLocation.Location);
        $scope.data.lowStockData.currentPage = 1;
        getData();

    });
    $scope.openProduct = function (product) {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: '/SPA/Views/ViewProductWindow.html',
            controller: 'ViewProductController',
            size: "",
            resolve: {
                items: function () {
                    console.log(product);
                    return product;
                }
            }
        }

        );
        modalInstance.result.then(function () {
            $scope.data.lowStockData.selectedItem = selectedItem;
        }, function () {
            console.log('Modal dismissed at: ' + new Date());
           // $log.info('Modal dismissed at: ' + new Date());
        });
    }



}
GridController.$inject = ['$scope','$uibModal'];

ViewProductController.js

console.log("View product controller is working");
var ViewProductController = function ($scope, $uibModalInstance, items) {
    console.log(items);
    $scope.selectedProduct = items;
    $scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
    $scope.series = ['Series A', 'Series B'];
    $scope.data = [
        [65, 59, 99, 73, 22, 11, 40],
        [76, 23, 65, 88, 45, 88, 98]
    ];

}
ViewProductController.$inject = ['$scope','$uibModalInstance'];

当我点击打开的产品时,会打开一个模态并显示图形,但$ scope.selectedProduct = items;总是返回undefined。我不知道为什么items对象从resolve块$ uibModal.open()返回undefined。请有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

由于itemsViewProductController的依赖项,但您尚未将其添加到$inject数组中以解析为依赖项。

更改:

ViewProductController.$inject = ['$scope','$uibModalInstance'];

致:

ViewProductController.$inject = ['$scope','$uibModalInstance', 'items'];

现在,它被添加为依赖关系,以便为ViewProductController解析。