I'm trying to pass a form input to my dialog (as title for example). The problem is: it don't get the form $scope
.
If I set the $scope
sinde the controller, it'll display normaly (see the $scope.text
for example). But, if I try to get the form $scope
(see `$scope.taskTitle) it just don't show anything. See my code:
JavaScript
app.controller('tasksCtrl', ['$scope', '$mdDialog', function($scope, $mdDialog){
$scope.teste = 'Just a test, dude';
$scope.expandTask = function() {
$mdDialog.show({
clickOutsideToClose: true,
controller: DialogController,
scope: $scope,
preserveScope: true,
templateUrl: 'models/dialog.tmpl.php',
locals: {
id: $scope.tasklist.id,
title: $scope.taskTitle
}
});
}
function DialogController($scope, $mdDialog, id, title) {
$scope.id = id;
$scope.title = title;
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
}
}]);
HTML
<div class="input-container float-icon" flex="100" layout="row" ng-repeat="task in tasklist">
<md-input-container flex="100">
<label>New Task...</label>
<input type="text" ng-model="taskTitle" name="taskTitle">
<md-button aria-label="Expandir Tarefa" class="md-icon-button expand-icon" ng-click="expandTask()">
<md-tooltip hide-sm>Expand Task</md-tooltip>
<i class="fa fa-expand"></i>
</md-button>
</md-input-container>
{{taskTitle}}
</div>
I'm trying to pass a form input to my dialog (as title for example). The problem is: it don't get the form $scope
.
If I set the $scope
sinde the controller, it'll display normaly (see the $scope.text
for example). But, if I try to get the form $scope
(see `$scope.taskTitle) it just don't show anything. See my code:
JavaScript
app.controller('tasksCtrl', ['$scope', '$mdDialog', function($scope, $mdDialog){
$scope.teste = 'Just a test, dude';
$scope.expandTask = function() {
$mdDialog.show({
clickOutsideToClose: true,
controller: DialogController,
scope: $scope,
preserveScope: true,
templateUrl: 'models/dialog.tmpl.php',
locals: {
id: $scope.tasklist.id,
title: $scope.taskTitle
}
});
}
function DialogController($scope, $mdDialog, id, title) {
$scope.id = id;
$scope.title = title;
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
}
}]);
HTML
<div class="input-container float-icon" flex="100" layout="row" ng-repeat="task in tasklist">
<md-input-container flex="100">
<label>New Task...</label>
<input type="text" ng-model="taskTitle" name="taskTitle">
<md-button aria-label="Expandir Tarefa" class="md-icon-button expand-icon" ng-click="expandTask()">
<md-tooltip hide-sm>Expand Task</md-tooltip>
<i class="fa fa-expand"></i>
</md-button>
</md-input-container>
{{taskTitle}}
</div>
Share
Improve this question
edited Jul 11, 2015 at 8:58
jakeforaker
1,6571 gold badge20 silver badges34 bronze badges
asked Jul 9, 2015 at 13:20
Matheus CostaMatheus Costa
251 silver badge4 bronze badges
2 Answers
Reset to default 6You should pass in the task
object from the ng-repeat into the ng-click.
For ex ng-click="expandTask($event, task)"
and in your $mdDialog controller, you will have access to that object:
app.controller('tasksCtrl', ['$scope', '$mdDialog', function ($scope, $mdDialog) {
$scope.expandTask = function (e, task) {
//ng-click="expandTask($event, task)"
$mdDialog.show({
clickOutsideToClose: true,
controller: function ($mdDialog) {
var vm = this;
vm.task = {};
vm.task = task; //your task object from the ng-repeat
$scope.hide = function () {
$mdDialog.hide();
};
$scope.cancel = function () {
$mdDialog.cancel();
};
},
controllerAs: 'modal',
templateUrl: 'models/dialog.tmpl.php',
parent: angular.element(document.body),
targetEvent: e
});
};
}]);
And in modal template you will access the task object with controllerAs notation, for example:
<h1> {{ modal.task.name }} <h1>
Keep it minimal
and create a controller with scope variables on the fly
$scope.expandTask = (e, task) =>
$mdDialog.show({
templateUrl: 'dialog.template.html',
controller: $scope => $scope.taskName = task.name
})
taskName
is now a $scope
variable and can be used in the dialog template