Reputation: 77
I am new to angularjs, and writing an application where a template(inventory.html) containing grid is opened with , it has a validate button at bottom of grid, which calls validate() to validate selected grid rows, before that i need a modal to popup and take user name as input and then process further.
my validate function is inside controller of template(i.e. inventory.html) . I have a app.js which contains routing info and controller.js which contains controllers for all templates.
My question is, if there is any way that I can open modal from validate(), take user input and proceed further without writing separate controller for modal. (I have a separate userinput.html for modal.)
(Sorry If my question is not clear. Plz help I am stuck with this and have tried many options from web)
// This is my controller
var app = angular.module('app', []);
app.controller('InventoryCtrl',['$scope','$location','$http','$modal',function($scope, $location, $http,$modal{console.log("Inside inventory ctrlr");
// Validate Function for validate button click
$scope.validate = function()
{
$scope.user = null;
$scope.build = null;
// Show modal to take user inputs for environment
var modalInstance = $modal.open(
{
controller : "inputModalCntrl",
templateUrl : "../partials/addEnvironment.html",
resolve: {
$callback: function () {
return function(param,user,build){
/* This function print value(param) dispached in modal controller */
console.log(param,user,build);
$scope.user = user;
$scope.build = build;
};
},
$send: function(){
return function(){
/* This function send param for the modal */
return {param1:"Hello Word Main"};
};
}
}
});
// This is further process of function
postdata = {};
var dlist = $scope.gridApi.selection.getSelectedRows();
postdata['dlist'] = dlist;
$http({ url: "/api/check", data: postdata, method: "POST" })
.success(function (response) { alert(response);})
.error(function () { alert('Error getting data');});
};
}]);
and this is modal controller
app.controller("inputModalCntrl", function($scope, $modalInstance, $callback, $send) {
$scope.init = function(){
/* This function print value(param) dispached in main controller */
console.log($send);
/* This send data to main controller */
$callback({param1:"Hello Word Modal", user :user, build : build});
};
});
Upvotes: 4
Views: 18037
Reputation: 77
This solved my problem plnkr. Thanks @Remigijus, @Emir Marques
$scope.open = function (user) {
$scope.user = user;
$modal.open({
templateUrl: 'myModalContent.html',
backdrop: true,
windowClass: 'modal',
controller: function ($scope, $modalInstance, $log, user) {
$scope.user = user;
$scope.submit = function () {
$log.log('Submiting user info.');
$log.log(JSON.stringify(user));
$modalInstance.dismiss('cancel');
}
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
},
resolve: {
user: function () {
return $scope.user;
}
}
});
};
Upvotes: 0
Reputation: 2687
Look this example:
/* Main controller */
App.controller("JustificativaModalController", function($scope, $modal) {
var modalInstance = $modal.open({
controller : "ControllerModal",
templateUrl : "TemplateModal.html",
resolve: {
$callback: function () {
return function(param){
/* This function print value(param) dispached in modal controller */
console.log(param);
};
},
$send: function(){
return function(){
/* This function send param for the modal */
return {param1:"Hello Word Main"};
};
}
}
});
});
/* Modal controller */
App.controller("JustificativaModalController", function($scope, $modalInstance, $callback, $send) {
$scope.init = function(){
/* This function print value(param) dispached in main controller */
console.log($send);
/* This send data to main controller */
$callback({param1:"Hello Word Modal"});
};
});
Upvotes: 0
Reputation: 96
I would strongly suggest you write a separate controller for you modal. That will greatly help you to maintain your code later.
If I understand correctly (from your tags) you use an this one: https://angular-ui.github.io/bootstrap/ From your main controller, open the modal:
var modalInstance = $modal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});
note the resolve function, you can pass extra parameters like this to modal controller.
In modal controller:
angular.module('your.awesomeApp').controller('ModalInstanceCtrl', function ($scope, $modalInstance, items) {
note the third parameter, it will be values, that you have passed.
Upvotes: 1
Reputation: 8971
You can use ui.bootstrap's components which include a modal. You won't have to use a separate controller.
Upvotes: 1