Using angularjs UI I'm trying to pass an argument to my modal.
使用angularjs UI我试图将参数传递给我的模态。
The thing is, item is undefined. I've checked out the documentation etc. and I don't get what I'm doing wrong here.
事情是,项目未定义。我已经检查了文档等,但我不知道我在这里做错了什么。
The controller 'newproject' is the one calling the following code:
控制器'newproject'是调用以下代码的控件:
var modalInstance = $modal.open({
templateUrl: 'app/projects/createTask.html',
controller: 'createtask as vm',
resolve: {
item: function () {
return 'itemValue';
}
}
});
Inside the 'createtask' controller I have the following code (the beginning is displayed here):
在'createtask'控制器中我有以下代码(这里显示的是开头):
(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?!?!?!
Note that the 'newproject' controller is located inside 'newProject.js' while the 'createtask' controller createTask.js.
请注意,'newproject'控制器位于'newProject.js'内,而'createtask'控制器位于createTask.js内。
For the sake of completeness, here are the complete controllers:
为了完整起见,这里有完整的控制器:
newProject.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:
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 个解决方案
#1
4
Try adding 'item' to the array in your controller definition:
尝试将“item”添加到控制器定义中的数组中:
angular.module('app').controller(controllerId,
['common', '$modalInstance', 'item', createtask]);
#2
2
I had the same issue and I found the item is undefined because there is no initial value.
我有同样的问题,我发现该项是未定义的,因为没有初始值。
$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');
};
});
#1
4
Try adding 'item' to the array in your controller definition:
尝试将“item”添加到控制器定义中的数组中:
angular.module('app').controller(controllerId,
['common', '$modalInstance', 'item', createtask]);
#2
2
I had the same issue and I found the item is undefined because there is no initial value.
我有同样的问题,我发现该项是未定义的,因为没有初始值。
$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');
};
});