Thursday 16 February 2017

NG2- ngFor create dynamic ID for the angular 2 for loop attribute based

NG2- ngFor create dynamic ID for the angular 2 for loop attribute based
=====================================================

You have to access label's attribute like this : [attr.for]="'ques-' + i"

and you have a bug in your ngFor, u lack of :

<div *ngFor="let question of questions; let i = index;">
<span [attr.id]="'ques-' + i">question </span>
</div>

Ref:
http://stackoverflow.com/questions/40239028/angular-2-dynamic-for-attribute-in-ngfor

NG2- router/URL change function call

NG2- router change function call
============================
import { Router } from '@angular/router'

@Component{

Export Class Mycomponent{

Constructor(_router: Router){

 _router.events.subscribe((val) => {
                this.searchChange = '';
}

}

Ref:http://stackoverflow.com/questions/33520043/how-to-detect-a-route-change-in-angular-2

NG2- Push object to an Array , pop, Remove Particular Object

NG2- Push object to an Array 
============================
@Component{

public Cart:Array = [];

product = {
"name": "shirt",
"price": "$32",
"productCode": "123123" 
 }

AddToCart(){   // add to cart (product)
let cardItem = {
"itemName":product.name
"itemPrice":product.price,
"itemKey": product.productCode
}
this.Cart.push(cardItem);       // Push object to Array.
console.log(this.Cart)
} // add method end


Remove Particular Item from Cart
================================

RemoveFromCart(paritucarItem){

let cardItem = {
"itemName":paritucarItem.name
"itemPrice":paritucarItem.price,
"itemKey": paritucarItem.productCode
}

let index = this.Cart.indexOf(cardItem);
this.Cart = this.Cart.filter( (x) => {
console.log('-----',x.productCode,'---',paritucarItem.productCode,'------',x.productCode === paritucarItem.productCode);
return x.productCode != paritucarItem.productCode;
});
console.log(this.Cart)       // Removed cart item

}

remove the last item
=====================

product = {
"name": "shirt",
"price": "$32",
"productCode": "123123" 
 }

lastItmeRemove(){
let cardItem = {
"itemName":product.name
"itemPrice":product.price,
"itemKey": product.productCode
}
this.Cart.pop(cardItem);       // pop object to Array.
console.log(this.Cart)
} // remove last item

JS,- JAVASCRIPT: REMOVE DUPLICATES FROM ARRAY OF OBJECTS

JS,NG2- JAVASCRIPT: REMOVE DUPLICATES FROM ARRAY OF OBJECTS
-------------------------------------------------------------
var tmp = [
    {
        "uniqueCode" : "1234",
        "selected" : true,
        "otherVal" : "abc"
    },
    {
        "uniqueCode" : "5678",
        "selected" : false,
        "otherVal" : "abc"
    },
    {
        "uniqueCode" : "1234",
        "selected" : true,
        "otherVal" : "def"
    }
];

function removeDuplicates(arr, prop) {
     var new_arr = [];
     var lookup  = {};

     for (var i in arr) {
         lookup[arr[i][prop]] = arr[i];
     }

     for (i in lookup) {
         new_arr.push(lookup[i]);
     }

     return new_arr;
 }

var uniqueArray = removeDuplicates(tmp, "uniqueCode");

console.log(uniqueArray);

Ref: http://www.tjcafferkey.me/remove-duplicates-from-array-of-objects/

Thursday 9 February 2017

NG2-Filter text to convert upper case character, even entering as lower case

NG2-Filter text to convert upper case character, even entering as lower case.
======================================================

template.html
================

<input  type="text" [ngModel]="sectionSearch" (ngModelChange)="onSearchSection($event)" 
#input (input)="input.value=$event.target.value.toUpperCase()" placeholder="Search...">
 
componets.ts
============
onSearchSection(event:Event) {

    this.allsection = this.toFilterAllSection.filter(
              section => section.displayName.toUpperCase().indexOf(event) >= 0
              );

    } 

Note:
data.section is backend data as an array.

Text encode format for multi language support in text field

Text encode format for multi language support in text field:
------------------------------------------------------------

Frong End -> encodeURIComponent(encodeURIComponent(searchKey))

Backend -> text=java.net.URLDecoder.decode(text, "UTF-8");

Friday 3 February 2017

NG2- Disable HTML Element by default, change elable on (click) button

NG2- Disable HTML Element by default, change elable on (click) button
===============================================
@component({
selector : 'app',
tempalte : `<h1 class="disable"> Header Title (click)="popup()" <h1>
<button (click)="enableHeader()"`,
styles : `.disable{
pointer-events: none;
opacity: 0.5;
}`
})

export class EnaleComponet {

enableHeader(){
document.getElementbyTag('h1).removeClass.disable;
}

popup(){
alert(click also enable);
}
}


I have tried with NG2 & jQuery

Thursday 2 February 2017

NG2- Model Popup close using Angular 2 component functionality

Model Popup close using Angular 2 functionality:
===========================================


soln: 1 jQuery based
=======
 jQuery variable with any type inside Angular2 controller.

public jQuery:any;

Add the above just after import statements and before component decorator.

Then access bootstrap modal with it's id like this.

jQuery("#myModal).modal("hide");



Soln:2: Typescript based
=======
<div id="myModal" [hidden]="hideModal" class="modal fade">

hideModal: boolean = false;

 set submitComments(), set hideModal to true
 submitComments(){

 this.hideModal = true;
 }

Ref: http://stackoverflow.com/questions/35354041/how-can-i-close-a-modal-in-angular-2