My modal dialog as a funciton in ts file - angularjs

Hello my fellow stackoverflowers,
I guess i have a simple question.
In my html i have a Modal Dialog that pops up. It uses ng2 Bootstrap. It works and all but... I want this code
"<button class="btn btn-primary" (click)="showModal()">Login</button>"
(showModal())
replace it in my ts file as a function
showModal(modal: ModalDirective) {
}
How can i do this? i am already struggling more then a hour.
This is my full HTML Code.
<button class="btn btn-primary" (click)="showModal()">Login</button>
<!-- Modal -->
<div bsModal #lgModal="bs-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" (click)="lgModal.hide()" aria-label="Close">
<span aria-hidden="true"> ×</span>
</button>
<h4 class="modal-title">Login</h4>
</div>
<div class="modal-body">
<form id="myForm" role="form" [ngFormModel]="CreateGroup">
<div class="form-group">
<div class="input-group">
<input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name' placeholder="Gebruikersnaam">
<label for="uLogin" class="input-group-addon glyphicon glyphicon-user"></label>
</div>
</div>
<div class="form-group">
<div class="input-group">
<input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password' placeholder="Wachtwoord">
<label for="uPassword" class="input-group-addon glyphicon glyphicon-lock"></label>
</div>
</div>
</form>
<div class="modal-footer">
<button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Login</button>
</div>
</div>
</div>
</div>
and hits is my full TS code
import { Component } from '#angular/core';
import { ROUTER_DIRECTIVES } from '#angular/router';
import { CORE_DIRECTIVES, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder } from '#angular/common';
import { ModalDirective, BS_VIEW_PROVIDERS} from 'ng2-bootstrap/ng2-bootstrap';
class DemoInfo {
name: string;
password: string;
}
#Component({
template: require('./template.component.html'),
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES, ModalDirective],
viewProviders: [BS_VIEW_PROVIDERS]
})
export class ModalComponent {
CreateGroup: ControlGroup;
demoInfo: DemoInfo;
modal: ModalDirective;
constructor(fb: FormBuilder) {
this.demoInfo = new DemoInfo();
this.CreateGroup = fb.group({
'name': new Control(this.demoInfo.name),
'password': new Control(this.demoInfo.password)
})
}
addNewGroup(demoInfo: DemoInfo) {
console.log(demoInfo, 'whole object');
this.demoInfo = new DemoInfo();
}
showModal(modal: ModalDirective) {
}
hideModal(modal: ModalDirective) {
}
}

So this is what i did.
I added #ViewChild('lgModal') modal: any; in my ModalComponent
made a new function called showModal()
and referenced it by calling this.modal.dosomething();

Related

Editing data in Modal in AngularJS

I have a problem with editing data in the bootstrap modal.
The problem is that in the modal, only first record from the database is displayed.
Modal only displays the problem because it retrieves data from 1 record.
My code AngularJS:
$scope.editLink = function( link ){
var linkdId = $routeParams.id;
$scope.id = linkdId;
$http({
method: 'get',
url: 'api/admin/links/get/' + linkdId
}).then(function (response){
$scope.edlink = response.data.records;
},function (error){
console.log('Blad we wczytywaniu danych');
});
}
View.html
Edit
<div class="modal fade" id="edit-data" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<form method="POST" name="editLinked" role="form" ng-submit="saveChanges( link )">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Edit Item</h4>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group">
<input type="text" class="form-control" ng-model="edlink.login">
<input type="text" class="form-control" ng-model="edlink.password">
<input type="text" class="form-control" ng-model="edlink.links">
</div>
</div>
</div>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" ng-disabled="editLinked.$invalid" class="btn btn-primary create-crud">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>

Bootstrap Modal with Angular 2

I am working on a modal with Angular 2. So far so good no errors but then... When i press on the button nothing appears. Am i missing something? Surely i do..
this is my template
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
{{demoInfo | json}}
</div>
<div class="modal-body">
<form class="form-horizontal" id='myForm' role="form" [ngFormModel]="CreateGroup">
<div class="col-md-7">
Name: <input type="text" [(ngModel)]='demoInfo.name' class="form-control" ngControl='name'>
</div>
<div class="col-md-7">
Password: <input type="password" [(ngModel)]='demoInfo.password' class="form-control" ngControl='password'>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" [disabled]='!CreateGroup.valid' (click)="addNewGroup(demoInfo)" class="btn btn-primary">Create</button>
</div>
</div>
</div>
</div>
this is my template component
import { Component } from '#angular/core';
import { ROUTER_DIRECTIVES } from '#angular/router';
import { CORE_DIRECTIVES, NgClass, FORM_DIRECTIVES, Control, ControlGroup, FormBuilder, Validators } from '#angular/common';
class DemoInfo {
name: string;
password: string;
}
#Component({
template: require('./template.component.html'),
directives: [FORM_DIRECTIVES, CORE_DIRECTIVES]
})
export class ModalComponent {
CreateGroup: ControlGroup;
demoInfo: DemoInfo;
constructor(fb: FormBuilder) {
this.demoInfo = new DemoInfo();
this.CreateGroup = fb.group({
'name': new Control(this.demoInfo.password),
'password': new Control(this.demoInfo.password)
})
}
addNewGroup(demoInfo: DemoInfo) {
console.log(demoInfo, 'whole object');
this.demoInfo = new DemoInfo();
}
}
Make sure have installed Bootstrap.js. Angular-ui won't work without it.
Make sure in your script tag the Bootstrap installs first.
src="~/Scripts/bootstrap.js"
src="~/Scripts/angular.js"
src="~/Scripts/app/app.js"
src="~/Scripts/ui-bootstrap-tpls-2.0.0.js"
Make sure you add a dependency to ui.bootstrap in your module (at least in Angular 1.5) I'm unfamiliar with Angular 2.
Let me know if this doesn't fix your issue.
I didn't add ng2-bootstrap library in my Angular js.
If you are new with Angular and you want to implement bootstrap js components the correct way i recommend to read the ng2 documentation.
I followed the steps on this link https://valor-software.com/ng2-bootstrap/#/

bootstrap validator doesn't want reset

I have a problem with bootstrap validator,
i have a angularjs project where i have a form in a modal in a directive.
so, i want when the modal hides, reset the form to the validator reset.
But when i use :
$('#StandardForm').bootstrapValidator('resetForm', true);
nothing is happening, when i recall my form modal, the validators is always here.(the inputs are green(good) or red(not good)).
see my directive :
angular.module('app.administration').directive('wcStandardForm',['servicesHTTP', function(servicesHTTP)
{
return {
restrict: 'E',
scope: {
standard: '=',
method: '&'
},
replace: true,
templateUrl: 'app/administration/directives/standard/wc-standard-form.tpl.html',
link: function(scope, form)
{
form.bootstrapValidator({
framework: 'bootstrap',
excluded: ':disabled',
feedbackIcons: {
valid: 'glyphicon glyphicon-ok',
invalid: 'glyphicon glyphicon-remove',
validating: 'glyphicon glyphicon-refresh'
},
fields: {
name: {
validators: {
notEmpty: {
message: 'le nom est nécessaire.'
},
stringLength: {
max: 31,
message: 'Le nom ne doit pas dépasser 31 caractères.'
}
}
}
}
});
},
controller: function($scope)
{
/**
* Vide le moduleForm
*/
$scope.clearStandardForm = function()
{
//$scope.standard = {};
$('#StandardForm').bootstrapValidator('resetForm', true);
};
/**
* Valide le module et envoi la requete http
*/
$scope.validateStandardForm = function()
{
...
$scope.clearStandardForm();
};
}
};
}]);
and here the template :
<div class="modal fade" id="standardFormModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true" ng-click="clearStandardForm()">×</span>
<span ng-click="clearStandardForm()" class="sr-only">Close</span>
</button>
<h4 class="modal-title">Assigner fonctions</h4>
</div>
<div class="modal-body">
<form id="StandardForm" method="post" class="ng-pristine ng-valid bv-form" novalidate="novalidate">
<button type="submit" class="bv-hidden-submit" style="display: none; width: 0px; height: 0px;"></button>
<div class="form-group">
<label class="control-label">Nom</label>
<input type="text" class="form-control" name="name" ng-model="standard.standard_name">
</div>
<div class="form-actions">
<div class="row">
<div class="col-md-12">
<button type="button" class="btn btn-default" data-dismiss="modal" ng-click="clearStandardForm()">Close</button>
<button type="submit" class="btn btn-primary"data-dismiss="modal" ng-click="validateStandardForm()"><i class="fa fa-save"></i> Sauvegarder</button>
</div>
</div>
</div>
</form>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
I tried to do the $('#StandardForm').bootstrapValidator('resetForm', true); on the event show.bs.modal et hidden.bs.modal of the modal and the same things.
So if you have a idea, i will take it :)
Looking at your code, found this mistake id="StandardF orm" (if it's not typo)
<form id="StandardF orm" method="post" class="ng-pristine ng-valid bv-form" novalidate="novalidate">
Where the selector use in JS is $('#StandardForm')
Second if you look at the Reference here, alternate way to resets the form fields which have validator rules is $(form).data('bootstrapValidator').resetForm();
$('#StandardForm').data('bootstrapValidator').resetForm(true);

How to get an html content in modal-body

I'm having trouble passing a html (form) for body modal.
Use the modal as a service, and have a standard template for the modal. If not past the template will of course use the default. And thus fail to repeat code.Then I wanted to go into the modal only the content that will be passed in scope.
modal.service.js
(function() {
var app = angular.module('app');
function ModalService($rootScope, $q, $modal) {
var defaultOptions = getModalDefaultOptions();
var service = {
showModal: showModal
};
return service;
function getModalDefaultOptions() {
return {
templateUrl: '/app/shared/modals/templates/modal.default.html',
closeButtonText: 'Fechar',
actionButtonText: 'OK',
headerTitle: 'Header default',
bodyContent: 'Conteúdo body modal',
showActionButton: true,
showCloseButton: true,
headerClass: ''
};
}
function showModal(customModalOptions) {
var deferred = $q.defer();
//Create temp objects to work with since we're in a singleton service
var tempModalOptions = {};
//Map modal.html $scope custom properties to defaults defined in service
angular.extend(tempModalOptions, defaultOptions, customModalOptions);
var scope = $rootScope.$new();
scope.modalOptions = tempModalOptions;
scope.modalOptions.ok = function(result) {
deferred.resolve();
modal.hide();
};
scope.modalOptions.close = function(result) {
deferred.reject();
modal.hide();
};
var modal = $modal({
scope: scope,
templateUrl: scope.modalOptions.templateUrl,
title: scope.modalOptions.headerTitle,
content: scope.modalOptions.bodyContent,
show: true
});
console.log(tempModalOptions);
return deferred.promise;
}
}
app.factory('ModalService', ModalService);
})();
modal.default.html
<div class="modal" role="dialog" tabindex="-1" data-type="Modal Default">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" data-ng-class="modalOptions.headerClass" ng-show="title">
<button class="close" ng-click="modalOptions.close();" type="button">×</button>
<h4 class="modal-title" ng-bind="modalOptions.headerTitle"></h4>
</div>
<div class="modal-body" ng-bind="modalOptions.contentTemplate">{{modalOptions.contentTemplate}}</div>
<div class="modal-footer">
<button class="btn btn-default" data-ng-click="modalOptions.close();" data-ng-show="modalOptions.showCloseButton" type="button">
{{modalOptions.closeButtonText}}</button>
<button class="btn btn-primary" data-ng-click="modalOptions.ok();" data-ng-show="modalOptions.showActionButton">
{{modalOptions.actionButtonText}}</button>
</div>
</div>
</div>
</div>
entidades.controller.js
(function() {
'use strict';
var app = angular.module('app');
app.controller('EntidadesController', EntidadesController);
function EntidadesController(EntidadeService, ModalService) {
var vm = this;
vm.entidades = JSON.parse(EntidadeService.getAll());
vm.openEditEntidades = function() {
ModalService.showModal({
headerClass: 'danger',
contentTemplate: '/app/modulos/entidades/views/edit-entidades.html'
})
.then(function() {
alert('Alert Modal 2');
});
};
// console.log('Entidades onDemand');
}
})();
The view that calls the modal
<section ng-controller="EntidadesEditCtrl as ctrl">
<div class="modal-header">
<h3 class="modal-title">Editando entidade</h3>
</div>
<div class="row">
<div class="modal-body">
<form novalidate class="form-inline">
<div class="col-md-6">
<div class="form-group">
<label class="sr-only" for="fm_name">Email address</label>
<input class="form-control" id="fm_name" ng-model="vm.entidade.name" type="text">
</div>
<div class="form-group">
<label class="sr-only" for="fm_email">Email</label>
<input class="form-control" id="fm_email" ng-model="vm.entidade.email" type="email">
</div>
<div class="form-group">
<label class="sr-only" for="fm_sexo">Sexo</label>
<input class="form-control" id="fm_sexo" ng-model="vm.entidade.gender" type="text">
</div>
<div class="form-group">
<label class="sr-only" for="fm_companhia">Companhia</label>
<input class="form-control" id="fm_companhia" ng-model="vm.entidade.company" type="text">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="sr-only" for="fm_id">User ID</label>
<input class="form-control" id="fm_id" ng-model="vm.entidade._id" type="text">
</div>
<div class="form-group">
<label class="sr-only" for="fm_phone">Telefone</label>
<input class="form-control" id="fm_phone" ng-model="vm.entidade.phone" type="tel">
</div>
<div class="form-group">
<label class="sr-only" for="fm_endereco">Endereço</label>
<input class="form-control" id="fm_endereco" ng-model="vm.entidade.address" type="text">
</div>
<div class="checkbox">
<label>
<input type="checkbox" ng-model="vm.entidade.isActive">{{vm.entidade.isActive}}
</label>
</div>
</div>
</form>
</div>
</div>
<br>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ctrl.update(vm.entidade); vm.ok()" type="button">OK</button>
<button class="btn btn-warning" ng-click="vm.cancel()" type="button">Cancel</button>
</div>
</section>
and the content to be inserted into body-modal
<div class="row" ng-controller="EntidadesController as ectrl">
<div class="row text-center">
<h1>Gerenciar entidades</h1>
</div>
<hr>
<div class="row col-md-10 col-md-offset-1">
<table class="table table-striped" st-table="rowCollection">
<thead>
<div class="panel ">
<div class="panel-heading">
<button class="btn btn-default pull-right" ng-click="open()">Add</button>
<div class="clearfix"></div>
</div>
</div>
<tr>
<th>#</th>
<th>Nome</th>
<th>Compania</th>
<th>Telefone</th>
<th>email</th>
<th>ação</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="entidade in ectrl.entidades">
<td>{{entidade._id}}</td>
<td>{{entidade.name}}</td>
<td>{{entidade.company}}</td>
<td>{{entidade.phone}}</td>
<td>{{entidade.email}}</td>
<td>
<button class="btn btn-default" ng-click="ectrl.openEditEntidades(entidade)" type="button">
Editar
</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td class="text-center" colspan="5">
<div st-displayed-pages="7" st-items-by-page="itemsByPage" st-pagination=""></div>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
Thanks for u time.
You should use ng-include to insert another template.
<div class="modal" role="dialog" tabindex="-1" data-type="Modal Default">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" data-ng-class="modalOptions.headerClass" ng-show="title">
<button class="close" ng-click="modalOptions.close();" type="button">×</button>
<h4 class="modal-title" ng-bind="modalOptions.headerTitle"></h4>
</div>
<div class="modal-body" ng-include="modalOptions.contentTemplate"></div>
<div class="modal-footer">
<button class="btn btn-default" data-ng-click="modalOptions.close();" data-ng-show="modalOptions.showCloseButton" type="button">
{{modalOptions.closeButtonText}}</button>
<button class="btn btn-primary" data-ng-click="modalOptions.ok();" data-ng-show="modalOptions.showActionButton">
{{modalOptions.actionButtonText}}</button>
</div>
</div>
</div>
</div>

Why an undefined is not a function with Angular directive to close Bootstrap modal

As a newbie to Angularjs I copied a directive from here: Closing Twitter Bootstrap Modal From Angular Controller, to be able to close a bootstrap modal from a controller, and in my implementation I'm getting an undefined is not a function error. (http://jsfiddle.net/jlamont/7f6dH/6/)
Here's the html
<div ng:app="app">
<div>
<ul><li>Login</li></ul>
<div id='loginModal' class='modal fade' myModal ng-controller="LoginDialogController" tabindex="-2" role="dialog" aria-labelledby="loginModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" ng-click="cancel()">x</button>
<h3>Login MyApp</h3>
</div>
<div class="modal-body">
<form name="login_form">
<alert ng-repeat="alert in alerts" type="danger" close="closeAlert($index)">{{alert.msg}}</alert>
<p><input type="text" class="span3" ng-model="login.user_email" placeholder="Email" required></p>
<p><input type="password" class="span3" ng-model='login.password' placeholder="Password" required></p>
<p><button type="submit" class="btn btn-primary" ng-click="ok()">Sign in</button>
Forgot Password?
</p>
</form>
</div>
<div class="modal-footer">
My.com?
Register
</div>
</div>
</div>
</div>
</div>
</div>
Here's the js
angular.module("app", []).directive('myModal', function() {
return {
restrict: 'A',
scope:{},
link: function(scope, element, attr) {
scope.dismiss = function() {
element.modal('hide');
};
}
}
});
function LoginDialogController($scope){
$scope.ok = function(){
$scope.dismiss();
};
}

Resources