How to change $scope variable from a modal window - angularjs

I have this function inside my controller to which I send a series of data that I receive as parameters correctly.
$scope.changeValues = function(measure,name,valCol) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
backdrop: 'static',
scope: $scope,
controller: function($scope, $modalInstance) {
$scope.ok = function(){
$scope.myVar = "hello";
console.log($scope.myVar);
modalInstance.close();
}
}
});};
I want to change the value of the $scope.myVar variable in my $modal.window, but these changes are not saved. Where is the error?
<div class="reducedfont" ng-init="loadCtrl()">
<script type="text/ng-template" id="myModalContent.html">
<!-- template for modal -->
<div class="modal-header">
<h3 class="modal-title">{{valueName}}</h3>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">Ok</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div></script>
<p>{{myVar}}</p>

if you want to change the
$scope.changeValues = function(measure,name,valCol) {
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
backdrop: 'static',
scope: $scope,
controller: function($scope, $modalInstance) {
$scope.ok = function(){
$scope.myVar = "hello";
console.log($scope.myVar);
$modalInstance.close($scope.myVar);
}
}
})//end of $model
modalInstance.result.then(function (SelectedItem) {
setTimeout(function () {
//SelectedItem is the value return from modal controller on close and $scope.myVar is the variable in the controller where $scope.changeValues function is defined.
$scope.myVar= SelectedItem;
}, 500);
});
};//end of function

Ok, you are doing well but in a wrong way.
If you want to keep the updated value you have to set it as an object property value.
Working with string (or in general with primitives type) breaks the object reference between your scopes.
Try with:
controller: function($scope) {
$scope.ok = function(){
$scope.myVar.value = "hello";
console.log($scope.myVar);
modalInstance.close();
}
}
When you will be out of your modal (and when its scope is destroyed) your $scope.myVar will keeping that property value with the update.

Related

binding template view with uibmodal controller

I use uibmodal in one of my controller, and managed to pass data to the modal controller.
However, once the data has been passed in my modal controller, I can't figure out how to get this data rendered in the modal template.
My main controller:
doEdit = function () {
var modalScope = $scope.$new(false);
modalScope.roleModel = self.gridApiRoles.selection.getSelectedRows()[0];
var modalInstance = $uibModal.open({
templateUrl: 'views/dialog.html',
scope: modalScope,
windowTemplateUrl: 'template/flexModal.html',
backdrop: 'static',
resolve: {
roleModelScope: function () {
return modalScope.roleModel;
}
},
controller: ['$scope', '$rootScope', 'roleModelScope', DialogEditController],
controllerAs: 'ctrl'
});
modalScope.modalInstance = modalInstance;
modalInstance.result.then(
function close(result) {
console.info(result);
},
function dismiss() {
console.info("dialog dismissed");
}
);
}
};
My UibModal controller:
let DialogEditController = function ($scope, $rootScope, roleModelScope) {
let self = this;
self.$onInit = () => {
initTest();
};
let initTest = () => {
console.log(roleModelScope.name);
};
};
At this point, roleModelScope.name has been perfectly passed to the modal controller.
My template:
<div class="modal-dialog" style="width:900px; height:750">
<div class="modal-content">
<div class="modal-header bg-info">
</div>
<div class="modal-body">
{{roleModelScope.name}}
</div>
<div class="modal-footer">
</div>
</div>
</div>
I tried to use ctrl.roleModelScope but it didn't work neither.
Thank you for your answers.
Assign a roleModelScope value inside controller this(context), so that it will be available for binding on view.
Also your Modal controller alias is ctrl, use {{ctrl.roleModelScope.name}} inspite of {{roleModelScope.name}}.
<div class="modal-body">
{{ctrl.roleModelScope.name}}
</div>
Code
let initTest = () => {
this.roleModelScope = roleModelScope;
console.log(roleModelScope.name);
};

Passing modal data in AngularJS using controller as and vm

I have a pair of controllers that looks like this:
(function () {
'use strict';
angular
.module('room')
.controller('RoomGetCtrl', Room)
.controller('TestCtrl', Test)
Room.$inject = [...'$uibModal'...];
Test.$inject = [...'$uibModalInstance'...];
function Room(..., $scope, $uibModal) {
var vm = this;
...
vm.open = function (size) {
vm.modalInstance = $uibModal.open({
templateUrl: 'modal.html',
controller: 'TestCtrl as vm',
});
};
}
function Test(???){
this.modalText = 'Modal Text'
this.modalCancel = function() {
???.dismiss();
}
}
})();
The view looks like this:
<script type="text/ng-template" id="modal.html">
<div class="modal-header">
<h3 class="modal-title">Modal window</h3>
</div>
<div class="modal-body">
<pre>{{ vm.modalText }}</pre>
</div>
<div class="modal-footer">
<button class="btn btn-default" ng-click="vm.modalCancel()">Cancel</button>
</div>
</script>
<button type="button" class="btn btn-default" ng-click="vm.open()">Open me!</button>
...
The above works, except that I'm unable to figure out what goes in the ??? in Test() above. I've tried a variety of things, whenever I click the cancel button, the console logs an error along the lines of "x.dismiss is not a function," where "x" is whatever I've tried use.
Any help?
Nevermind. I instantaneously worked it out myself (I swear, posting the question makes me think better). Anyway, here:
(function () {
'use strict';
angular
.module('room')
.controller('RoomGetCtrl', Room)
.controller('TestCtrl', Test)
Room.$inject = [...,'$uibModal'];
Test.$inject = [$uibModalInstance];
function Room(..., $uibModal) {
/*jshint validthis: true */
var vm = this;
...
vm.open = function () {
vm.modalInstance = $uibModal.open({
templateUrl: 'modal.html',
controller: 'TestCtrl as vm',
});
};
}
function Test($uibModalInstance){
this.modalText = 'Modal Text'
this.modalCancel = function() {
$uibModalInstance.dismiss();
}
}
})();
Really simple. $uibModalInstance.dismiss();. It was right there in the documentation
sigh

UI Bootstrap Modal within Directive

I'm using Angular 1.4.1 and UI Bootstrap 0.13.
I have a directive from which I'm opening a modal. The modal opens fine, but the buttons seemingly don't get bound to their handlers - they don't do anything. I've used this same code in another project just fine, except not from within a directive.
My directive:
(function () {
var app = angular.module('myApp');
app.directive('someDirective', function () {
return {
restrict: 'E',
templateUrl: 'SomeDirective.html',
scope: {
list1: '=list1',
list2: '=list2',
save: '&'
},
controller: ['$scope','$modal','myService', function ($scope,$modal,myService) {
$scope.openModal = function () {
var modalInstance = $modal.open({
templateUrl: 'ModalTemplate.html',
controller: 'modalController',
backdrop: 'static',
size: 'sm',
resolve: {
saveData: function () {
//do save action
}
}
});
modalInstance.result.then(
function (itemToSave) {
//save item
},
function () {
//Cancel
}
);
};
}]
}
});
}());
The modal's controller:
(function() {
var app = angular.module('myApp');
app.controller('modalController', [
'$scope', '$modalInstance', 'saveData',
function($scope, $modalInstance, saveData) {
$scope.saveData = saveData;
$scope.save = function() {
$modalInstance.close($scope.saveData);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
}
]);
}());
And the template for the modal content:
<div class="modal-header bg-info">
<h3 class="modal-title">Add New Record</h3>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form"></form>
</div>
<div class="modal-footer bg-info">
<button class="btn btn-primary" ng-click="save()">Save</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
My thought is that I'm having issues with scope, but I can't track it down. I have put break points on modalController. The app.controller() call happens when the app loads, I've seen that. But breakpoints within save() and cancel() never get hit.
Can someone help me figure out why the modal's buttons don't do anything?
This turned out to be a stupid mistake. At some point I apparently overwrote the name of one of the other controllers in my project with the same name of the controller I was using for the modal. The other controller did not have save() or cancel() methods so nothing was happening. As soon as I fixed my error and all controllers once again had their proper names this started working again.

Why $scope variable isn't updated?

I have a problem with a scope variable. When i open the form, the content is displayed correctly in the input field. But when I change them and the ok-function fires the changes are not displayed. instead i get the content of form.name. Why isnt the scope updated by the ng-model of the input field?
app.controller('updateFormCtrl', function( $scope, $modalInstance, APIService, form) {
$scope.name = form.name;
$scope.ok = function() {
alert($scope.name)
};
});
View:
<div class="col-sm-10"><input ng-model="name" type="text" class="form-control" placeholder="Categorie"></div>
Edit: Here is the other controller:
$scope.updateForm = function( form ) {
var modalInstance = $modal.open({
templateUrl: 'views/modals/updateForm.html',
controller: 'updateFormCtrl',
resolve: {
form: function() {
return form;
}
}
});
modalInstance.result.then(function() {
$scope.categories = APIService.query({ route:'category' });
});
};
Edit 2:
<div class="modal-footer">
<button type="button" class="btn btn-default" ng-click="cancel()">Abbrechen</button>
<button type="button" class="btn btn-primary" ng-click="ok()">Speichern</button>
</div><!-- /.modal-footer -->
It's because your $scope.ok isn't defined inside updateFormCtrl. So basically, your ok() cannot read the changes you have made in textbox.
Try moving $scope.ok inside controller.
app.controller('updateFormCtrl', function( $scope, $modalInstance, APIService, form) {
$scope.name = form.name;
$scope.ok = function() {
alert($scope.name)
};
});
I think this is the solution:
https://egghead.io/lessons/angularjs-the-dot

AngularJS bootstrap modal scope value not displayed

I'm using angular bootstrap modal.
termsText is populated and the scope.productTerms does contain a value. But for some reason when I output {{ productTerms }} inside the modal the value is not being displayed. Why?
js
$scope.openProductTerms = function (termsText) {
$scope.productTerms = termsText <-- has a value in console.log()
var modalInstance = $modal.open({
templateUrl: 'myModalTerms.html',
controller: ModalInstanceCtrl
});
var ModalInstanceCtrl = function ($scope, $modalInstance) {
$scope.ok = function () {
$modalInstance.dismiss('OK');
};
};
html
{{ productTerms }} < ==== value shows outside modal
<script type="text/ng-template" id="myModalTerms.html">
<div class="modal-body">
{{ productTerms }} <==== same value does not show insdie modal?
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
</div>
</script>
The modal has a new controller and therefore a new scope.
services.js
angular.module('services',[]).
factory('sharedService', function($rootScope) {
var sharedService = {};
sharedService.value = null;
sharedService.prepForBroadcast = function(value) {
this.value = value;
this.broadcastItem();
};
sharedService.broadcastItem = function() {
$rootScope.$broadcast('shared');
};
return sharedService;
});
controller.js
$scope.openProductTerms = function (termsText) {
sharedService.prepForBroadcast(termsText);
var modalInstance = $modal.open({
templateUrl: 'myModalTerms.html',
controller: ModalInstanceCtrl
});
lastController.js
var ModalInstanceCtrl = function ($scope, $modalInstance,sharedService) {
$scope.$on('shared', function() {
$scope.productTerms = sharedService.value;
});
$scope.ok = function () {
$modalInstance.dismiss('OK');
};
};
Try using {{$parent.productTerms}} within your HTML
From my experience, it seems that the angular bootstrap modal creates a new scope. So by referencing $parent first, you'll be able to get your model's value.

Resources