make div dragable while hold in angular5 - angular5

today i have a div which name is "title" ,
I have the task of making it move, so now I can do it in javascript with 'dragable', but I am using angular 5 and my posture is as follows :
import { Component, OnInit, Input } from '#angular/core';
import{ AppComponent } from '../app.component'
#Component({
selector: 'app-modal',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.css']
})
export class ModalComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
you can see my html and css in code pen https://codepen.io/__knif147/pen/yKjmao
So, i have to give it move when i hold the 'title ' div, somebody else help me please.

http://embed.plnkr.co/JbG8Si
go through this link.demo for drag and drop.
you have to install npm for that and put reference in app.module.ts in your project (npm install ng2-dnd --save) provided by angular

Related

Ionic PDFJS Cannot match any routes. URL Segment: 'lib/ui/index.html'

I am trying to open a pdf in my ionic 5 application with the plugin
npm i # pdftron / pdfjs-express --save
but ionic shows me the error Cannot match any routes. URL Segment: 'lib / ui / index.html'
please how to correct this error?
my code:
app.component.html
<div class="page">
<div class="header">Angular sample</div>
<div #viewer class="viewer"></div>
</div>
app.component.ts
import { Component, ViewChild, OnInit, ElementRef, AfterViewInit } from '#angular/core';
import WebViewer from '#pdftron/pdfjs-express';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, AfterViewInit {
#ViewChild('viewer', { static: false }) viewer: ElementRef;
wvInstance: any;
ngAfterViewInit(): void {
WebViewer({
path: '../lib',
initialDoc: '../files/webviewer-demo-annotated.pdf'
}, this.viewer.nativeElement).then(instance => {
this.wvInstance = instance;
})
}
ngOnInit() {
}
}
Have you used any of our angular samples here https://github.com/PDFTron?q=angular? You can clone one of the samples and take a look at how it is implemented

Angular template rendering non-encoded URL

How do I render a URL which is not encoded. Below is a sample
import { Component } from '#angular/core';
import { Router } from '#angular/router';
#Component({
selector: 'demo-app',
template: `<a [routerLink]="stringURL">Click here</a>`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
stringURL:string;
constructor(){
this.stringURL = "/url;mode=3"
}
}
The URL in the template has encoded string like /url%3Bmode%3D3 and I want it like /url;mode=3
How can I achieve this.
Here's the sample : https://stackblitz.com/edit/angular-q6mf3p
Thanks
You have to disable angular default URL encoding.
This post explains the solution quite well.
If the link is static, you can use the directive as follows: link to user component
If you use dynamic values to generate the link, you can pass an array of path segments, followed by the params for each segment.
For instance ['/url',{mode: 3}] means that we want to generate a link to /url;mode=3.
you code will work see this new link click to see
import { Component } from '#angular/core';
import { Router } from '#angular/router';
#Component({
selector: 'demo-app',
template: `<a [routerLink]="stringURL">Click here</a>`,
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
stringURL:any;
constructor(){
this.stringURL = ["/url",{mode:3}]
}
}

Ionic3 lazy loading a Component in Page

I am working to an app that will be like a ppt presentation with a homepage, then slides(which are lazyloaded), each slide will have a navigation bar with: prev, home and next btn.
SO I thought: if that navigation will be common for all slides why not have it like a separate component, with a navCtrl and nextSLide will be transmitted like an #Input(), goHome- will be a seRoot() and back will be navCtrl.pop() - navPop directly in html. I will post code below :
slide1.ts
#IonicPage()
#Component({
selector: 'page-slide1',
templateUrl: 'slide1.html',
})
export class Slide1Page {
link:string = "Slide2Page"
}
slide1.htm
<ion-content padding>
<app-nav [toSlide]="link"></app-nav>
</ion-content>
nav.ts
#IonicPage()
#Component({
selector: 'app-nav',
templateUrl: 'nav.html'
})
export class NavComponent {
#Input() toSlide;
constructor(public navCtrl: NavController) {}
}
nav.html
<button class="nav_left" navPop></button>
<button class="to_home" (click)="goHome()"></button>
<button class="nav_right" [navPush]="toSlide"></button>
nav.module.ts I've made it IonicPagemodule
import { NgModule } from '#angular/core';
import { IonicPageModule } from 'ionic-angular';
import { NavComponent } from './nav';
#NgModule({
declarations: [
NavComponent,
],
imports: [
IonicPageModule.forChild(NavComponent),
],
exports: [
NavComponent
]
})
export class NavComponentModule {}
When I navigate from homePage to slide1Page I got a long error(chunk of it here):
Uncaught (in promise): Error: Template parse errors: Can't bind to 'toSlide' since it isn't a known property of 'app-nav'. 1. If 'app-nav' is an Angular component and it has 'toSlide' input, then verify that it is part of this module. 2. If 'app-nav' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '#NgModule.schemas' of this component to suppress this message.
I want to know if my approach is good or doable, cuz I don't wanna spend time for nothing. Thanks!
Ok, so there error was because I didn't include NavCOmponent in slide1.module.ts
import { NavComponent } from './../../components/nav/nav';
import { NgModule } from '#angular/core';
import { IonicPageModule } from 'ionic-angular';
import { Slide1Page } from './slide1';
#NgModule({
declarations: [
Slide1Page,
NavComponent
],
imports: [
IonicPageModule.forChild(Slide1Page),
],
exports: [
Slide1Page,
NavComponent
]
})
export class Slide1PageModule {}
Still - is this a good approach?

Accessing angular 2 directive attached to component selector

I could use some help.
Lets say i have a directive:
import { Directive } from '#angular/core';
#Directive( {
selector: '[appMyDirective]'
} )
export class MyDirective {
constructor () {
}
seyHey () {
console.log( 'hey hey!' );
}
}
And i have a component
import { Component, OnInit, ViewChild } from '#angular/core';
import { MyDirective } from "../my-directive.directive";
#Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
#ViewChild(MyDirective) appMyDirective: MyDirective;
constructor() { }
ngOnInit() {
this.appMyDirective.seyHey();
}
}
Component template:
<p appMyDirective>
my-component works!
</p>
app.component.html
<app-my-component></app-my-component>
All good. I get hey hey on console. But i want to attach directive to component selector like so:
<app-my-component appMyDirective></app-my-component>
And be able to call directive methods inside component class. How?
Tnx for your time
Try accessing it like you would a Provider (in the constructor).
In your example...
import { Component, OnInit, Optional } from '#angular/core';
import { MyDirective } from "../my-directive.directive";
#Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponentComponent implements OnInit {
constructor(#Optional() private appMyDirective: MyDirective) { }
ngOnInit() {
this.appMyDirective.seyHey();
}
}
I've added the #Optional in case the directive is optional on your component, which may occur in some cases.
Oh, and this also works on Directive to Directive..
#Directive({....})
export class MyDirective {
constructor(#Optional() private appMyDirective: MyDirective) { }
}
This was tested on my project and works. The property was even ready in the constructor. Just keep an eye on any cyclical references (e.g. component calls the directive, the directive calls the component, etc..)
Happy Coding.

Angular 2 equivalent of ng-bind-html, $sce.trustAsHTML(), and $compile?

In Angular 1.x, we could insert HTML in real-time by using the HTML tag ng-bind-html, combined with the JavaScript call $sce.trustAsHTML(). This got us 80% of th way there, but wouldn't work when Angular tags were used, such as if you inserted HTML that used ng-repeat or custom directives.
To get that to work, we could use a custom directive that called $compile.
What is the equivalent for all of this in Angular 2? We can bind using [inner-html] but this only works for very simple HTML tags such as <b>. It doesn't transform custom angular 2 directives into functioning HTML elements. (Much like Angular 1.x without the $compile step.) What is the equivalent of $compile for Angular 2?
In Angular2 you should use DynamicComponentLoader to insert some "compiled content" on the page. So for example if you want to compile next html:
<div>
<p>Common HTML tag</p>
<angular2-component>Some angular2 component</angular2-component>
</div>
then you need to create component with this html as a template (let's call it CompiledComponent) and use DynamicComponentLoader to insert this component on the page.
#Component({
selector: 'compiled-component'
})
#View({
directives: [Angular2Component],
template: `
<div>
<p>Common HTML tag</p>
<angular2-component>Angular 2 component</angular2-component>
</div>
`
})
class CompiledComponent {
}
#Component({
selector: 'app'
})
#View({
template: `
<h2>Before container</h2>
<div #container></div>
<h2>After conainer</h2>
`
})
class App {
constructor(loader: DynamicComponentLoader, elementRef: ElementRef) {
loader.loadIntoLocation(CompiledComponent, elementRef, 'container');
}
}
Check out this plunker
UPD You can create component dynamically right before the loader.loadIntoLocation() call:
// ... annotations
class App {
constructor(loader: DynamicComponentLoader, elementRef: ElementRef) {
// template generation
const generatedTemplate = `<b>${Math.random()}</b>`;
#Component({ selector: 'compiled-component' })
#View({ template: generatedTemplate })
class CompiledComponent {};
loader.loadIntoLocation(CompiledComponent, elementRef, 'container');
}
}
I personally don't like it, it's look like a dirty hack to me. But here is the plunker
PS Beware that at this moment angular2 is under active development. So situation can be changed at any time.
DynamicComponentLoader is deprecated, you can use ComponentResolver instead
You could use this directive, add pipes if you need additional data manipulation. It also allows for lazy loading, you don't need it in your case, but it's worth mentioning.
Directive(I found this code and made some changes, you can do that too to make it fit your taste or use it as is):
import { Component, Directive, ComponentFactory, ComponentMetadata, ComponentResolver, Input, ReflectiveInjector, ViewContainerRef } from '#angular/core';
declare var $:any;
export function createComponentFactory(resolver: ComponentResolver, metadata: ComponentMetadata): Promise<ComponentFactory<any>> {
const cmpClass = class DynamicComponent {};
const decoratedCmp = Component(metadata)(cmpClass);
return resolver.resolveComponent(decoratedCmp);
}
#Directive({
selector: 'dynamic-html-outlet',
})
export class DynamicHTMLOutlet {
#Input() htmlPath: string;
#Input() cssPath: string;
constructor(private vcRef: ViewContainerRef, private resolver: ComponentResolver) {
}
ngOnChanges() {
if (!this.htmlPath) return;
$('dynamic-html') && $('dynamic-html').remove();
const metadata = new ComponentMetadata({
selector: 'dynamic-html',
templateUrl: this.htmlPath +'.html',
styleUrls: [this.cssPath]
});
createComponentFactory(this.resolver, metadata)
.then(factory => {
const injector = ReflectiveInjector.fromResolvedProviders([], this.vcRef.parentInjector);
this.vcRef.createComponent(factory, 0, injector, []);
});
}
}
Example how to use it:
import { Component, OnInit } from '#angular/core';
import { DynamicHTMLOutlet } from './../../directives/dynamic-html-outlet/dynamicHtmlOutlet.directive';
#Component({
selector: 'lib-home',
templateUrl: './app/content/home/home.component.html',
directives: [DynamicHTMLOutlet]
})
export class HomeComponent implements OnInit{
html: string;
css: string;
constructor() {}
ngOnInit(){
this.html = './app/content/home/home.someTemplate.html';
this.css = './app/content/home/home.component.css';
}
}
home.component.html:
<dynamic-html-outlet [htmlPath]="html" [cssPath]="css"></dynamic-html-outlet>
After reading a lot, and being close of opening a new topic I decided to answer here just to try to help to others. As I've seen there are several changes with the latest version of Angular 2. (Currently Beta9)
I'll try to share my code in order to avoid the same frustration I had...
First, in our index.html
As usual, we should have something like this:
<html>
****
<body>
<my-app>Loading...</my-app>
</body>
</html>
AppComponent (using innerHTML)
With this property you will be able to render the basic HTML, but you won't be able to do something similar to Angular 1.x as $compile through a scope:
import {Component} from 'angular2/core';
#Component({
selector: 'my-app',
template: `
<h1>Hello my Interpolated: {{title}}!</h1>
<h1 [textContent]="'Hello my Property bound: '+title+'!'"></h1>
<div [innerHTML]="htmlExample"></div>
`,
})
export class AppComponent {
public title = 'Angular 2 app';
public htmlExample = ' <div>' +
'<span [textContent]="\'Hello my Property bound: \'+title"></span>' +
'<span>Hello my Interpolated: {{title}}</span>' +
'</div>'
}
This will render the following:
Hello my Interpolated: Angular 2 app!
Hello my Property bound: Angular 2 app!
Hello my Interpolated: {{title}}
AppComponent Using DynamicComponentLoader
There is a little bug with the docs, documented in here. So if we have in mind that, my code should look now like this:
import {DynamicComponentLoader, Injector, Component, ElementRef, OnInit} from "angular2/core";
#Component({
selector: 'child-component',
template: `
<div>
<h2 [textContent]="'Hello my Property bound: '+title"></h2>
<h2>Hello my Interpolated: {{title}}</h2>
</div>
`
})
class ChildComponent {
title = 'ChildComponent title';
}
#Component({
selector: 'my-app',
template: `
<h1>Hello my Interpolated: {{title}}!</h1>
<h1 [textContent]="'Hello my Property bound: '+title+'!'"></h1>
<div #child></div>
<h1>End of parent: {{endTitle}}</h1>
`,
})
export class AppComponent implements OnInit{
public title = 'Angular 2 app';
public endTitle= 'Bye bye!';
constructor(private dynamicComponentLoader:DynamicComponentLoader, private elementRef: ElementRef) {
// dynamicComponentLoader.loadIntoLocation(ChildComponent, elementRef, 'child');
}
ngOnInit():any {
this.dynamicComponentLoader.loadIntoLocation(ChildComponent, this.elementRef, 'child');
}
}
This will render the following:
Hello my Interpolated: Angular 2 app!
Hello my Property bound: Angular 2 app!
Hello my Property bound: ChildComponent title
Hello my Interpolated: ChildComponent title
End of parent: Bye bye!
I think all you have to do is set the element you want to have compiled html with the [innerHTML]="yourcomponentscopevar"
Angular provided DynamicComponentLoader class for loading html dynamically. DynamicComponentLoader have methods for inserting components. loadIntoLocation is one of them for inserting component.
paper.component.ts
import {Component,DynamicComponentLoader,ElementRef,Inject,OnInit} from 'angular2/core';
import { BulletinComponent } from './bulletin.component';
#Component({
selector: 'paper',
templateUrl: 'app/views/paper.html'
}
})
export class PaperComponent {
constructor(private dynamicComponentLoader:DynamicComponentLoader, private elementRef: ElementRef) {
}
ngOnInit(){
this.dynamicComponentLoader.loadIntoLocation(BulletinComponent, this.elementRef,'child');
}
}
bulletin.component.ts
import {Component} from 'angular2/core';
#Component({
selector: 'bulletin',
templateUrl: 'app/views/bulletin.html'
}
})
export class BulletinComponent {}
paper.html
<div>
<div #child></div>
</div>
Few things you need to take care of :
Don't call loadIntoLocation inside the constructor of class . Component view is not yet created when component constructor is called. You will get error -
Error during instantiation of AppComponent!. There is no component
directive at element [object Object]
Put anchorName #child in html otherwise you will get error.
Could not find variable child
Have a look at this module https://www.npmjs.com/package/ngx-dynamic-template
After a long research, only this thing helped me. The rest of the solutions seems to be outdated.