传递给$ modal的参数是未定义的?

时间:2014-09-19 15:17:14

标签: angularjs angular-ui angular-ui-bootstrap

使用angularjs UI我试图将参数传递给我的模态。

事情是,项目未定义。我已经检查了文档等,但我不知道我在这里做错了什么。

控制器' newproject'是调用以下代码的人:

 var modalInstance = $modal.open({
                templateUrl: 'app/projects/createTask.html',
                controller: 'createtask as vm',
                resolve: {
                    item: function () {
                        return 'itemValue';
                    }
                }
            });

在'创建任务'控制器我有以下代码(这里显示开头):

(function () {
        'use strict';

        var controllerId = 'createtask';

        angular.module('app').controller(controllerId,
            ['common', '$modalInstance', createtask]);

        function createtask(common, $modalInstance, item) {
        //why is item is undefined here?!?!?!

请注意' newproject'控制器位于newProject.js'内。而“创造任务”#39; controller createTask.js。

为了完整起见,这里有完整的控制器:

newProject.js:

(function () {
    'use strict';

    var controllerId = 'newproject';

    angular.module('app').controller(controllerId,
        ['$location', '$modal', '$routeParams', '$window', 'common', 'config', 'datacontext', 'utility', projectdetails]);

    function projectdetails($location, $modal, $routeParams, $window, common, config, datacontext, utility) {
        var vm = this;

        function editTask(task) {
            var modalInstance = $modal.open({
                templateUrl: 'app/projects/createTask.html',
                controller: 'createtask as vm',
                resolve: {
                    item: function () {
                        return 'itemValue';
                    }
                }
            });

            modalInstance.result.then(function (newTask) {
                task = newTask;
                vm.projectCost += newTask.cost;
            });
        }
    }
})();

createTask.js:

(function () {
    'use strict';

    var controllerId = 'createtask';

    angular.module('app').controller(controllerId,
        ['common', '$modalInstance', 'item', createtask]);

    function createtask(common, $modalInstance, item) {

        if (item) {
            alert(item);
        } else
            alert('undefined');
    }

})();

2 个答案:

答案 0 :(得分:4)

尝试添加'项目'到控制器定义中的数组:

angular.module('app').controller(controllerId,
        ['common', '$modalInstance', 'item', createtask]);

答案 1 :(得分:2)

我有同样的问题,我发现项目未定义,因为没有初始值。

$scope.showDialog = function () {
    var aModalInstance = $modal.open({
        templateUrl: "yourPage.html",
        controller: "yourModalCtrl",
        resolve: {
           item: function () {
               **return null;// get an undefined error message**
               **return {firstName:"Superman"} // this one should be ok**                   
           }
        }
    });

   aModalInstance.result.then(function (val) {
       //not important
   }, function () {
       $log.info('Modal dismissed at: ' + new Date());
   });
};

    ==>

app.controller("yourModalCtrl", function ($scope, $modalInstance, item) {            
    $scope.firstName = item.firstName;    

    $scope.ok= function () {
        $modalInstance.close({firstName:$scope.firstName});
    };

    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});