angular 2 (click) passing iteration variable and [class.selected] passing function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
The following is taken from ng-book 2
@Component({
selector: 'products-list',
directives: [ProductRow],
inputs: ['productList'],
outputs: ['onProductSelected'],
template: `
<div class="ui items">
<product-row
*ngFor="let myProduct of productList"
[product]="myProduct"
(click)='clicked(myProduct)'
[class.selected]="isSelected(myProduct)">
</product-row>
</div>
`
})
class ProductsList {
/**
* @input productList - the Product passed to us
*/
productList: Product;
/**
* @ouput onProductSelected - outputs the current
* Product whenever a new Product is selected
*/
onProductSelected: EventEmitter<Product>;
/**
* @property currentProduct - local state containing
* the currently selected `Product`
*/
currentProduct: Product;
constructor() {
this.onProductSelected = new EventEmitter();
}
clicked(product: Product): void {
this.currentProduct = product;
this.onProductSelected.emit(product);
}
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
}
@Component({
selector: 'product-row',
inputs: ['product'],
... not relevant to the question
`
})
class ProductRow {
product: Product;
}
Two questions,
Q1. where it says
(click)='clicked(myProduct)'
is the argument for clicked the same as the product property on ProductRow component? I am used to passing $event to clicked. Why pass "myProduct" instead?
Q2. where it says
[class.selected]="isSelected(myProduct)"
it seems like we are doing
[class.selected]="false"
for all products cuz none of them is selected initially.
How is angular able to call isSelected(myProduct) again and again? How does angular decide when to call isSelected?
angular
add a comment |
The following is taken from ng-book 2
@Component({
selector: 'products-list',
directives: [ProductRow],
inputs: ['productList'],
outputs: ['onProductSelected'],
template: `
<div class="ui items">
<product-row
*ngFor="let myProduct of productList"
[product]="myProduct"
(click)='clicked(myProduct)'
[class.selected]="isSelected(myProduct)">
</product-row>
</div>
`
})
class ProductsList {
/**
* @input productList - the Product passed to us
*/
productList: Product;
/**
* @ouput onProductSelected - outputs the current
* Product whenever a new Product is selected
*/
onProductSelected: EventEmitter<Product>;
/**
* @property currentProduct - local state containing
* the currently selected `Product`
*/
currentProduct: Product;
constructor() {
this.onProductSelected = new EventEmitter();
}
clicked(product: Product): void {
this.currentProduct = product;
this.onProductSelected.emit(product);
}
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
}
@Component({
selector: 'product-row',
inputs: ['product'],
... not relevant to the question
`
})
class ProductRow {
product: Product;
}
Two questions,
Q1. where it says
(click)='clicked(myProduct)'
is the argument for clicked the same as the product property on ProductRow component? I am used to passing $event to clicked. Why pass "myProduct" instead?
Q2. where it says
[class.selected]="isSelected(myProduct)"
it seems like we are doing
[class.selected]="false"
for all products cuz none of them is selected initially.
How is angular able to call isSelected(myProduct) again and again? How does angular decide when to call isSelected?
angular
add a comment |
The following is taken from ng-book 2
@Component({
selector: 'products-list',
directives: [ProductRow],
inputs: ['productList'],
outputs: ['onProductSelected'],
template: `
<div class="ui items">
<product-row
*ngFor="let myProduct of productList"
[product]="myProduct"
(click)='clicked(myProduct)'
[class.selected]="isSelected(myProduct)">
</product-row>
</div>
`
})
class ProductsList {
/**
* @input productList - the Product passed to us
*/
productList: Product;
/**
* @ouput onProductSelected - outputs the current
* Product whenever a new Product is selected
*/
onProductSelected: EventEmitter<Product>;
/**
* @property currentProduct - local state containing
* the currently selected `Product`
*/
currentProduct: Product;
constructor() {
this.onProductSelected = new EventEmitter();
}
clicked(product: Product): void {
this.currentProduct = product;
this.onProductSelected.emit(product);
}
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
}
@Component({
selector: 'product-row',
inputs: ['product'],
... not relevant to the question
`
})
class ProductRow {
product: Product;
}
Two questions,
Q1. where it says
(click)='clicked(myProduct)'
is the argument for clicked the same as the product property on ProductRow component? I am used to passing $event to clicked. Why pass "myProduct" instead?
Q2. where it says
[class.selected]="isSelected(myProduct)"
it seems like we are doing
[class.selected]="false"
for all products cuz none of them is selected initially.
How is angular able to call isSelected(myProduct) again and again? How does angular decide when to call isSelected?
angular
The following is taken from ng-book 2
@Component({
selector: 'products-list',
directives: [ProductRow],
inputs: ['productList'],
outputs: ['onProductSelected'],
template: `
<div class="ui items">
<product-row
*ngFor="let myProduct of productList"
[product]="myProduct"
(click)='clicked(myProduct)'
[class.selected]="isSelected(myProduct)">
</product-row>
</div>
`
})
class ProductsList {
/**
* @input productList - the Product passed to us
*/
productList: Product;
/**
* @ouput onProductSelected - outputs the current
* Product whenever a new Product is selected
*/
onProductSelected: EventEmitter<Product>;
/**
* @property currentProduct - local state containing
* the currently selected `Product`
*/
currentProduct: Product;
constructor() {
this.onProductSelected = new EventEmitter();
}
clicked(product: Product): void {
this.currentProduct = product;
this.onProductSelected.emit(product);
}
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
}
@Component({
selector: 'product-row',
inputs: ['product'],
... not relevant to the question
`
})
class ProductRow {
product: Product;
}
Two questions,
Q1. where it says
(click)='clicked(myProduct)'
is the argument for clicked the same as the product property on ProductRow component? I am used to passing $event to clicked. Why pass "myProduct" instead?
Q2. where it says
[class.selected]="isSelected(myProduct)"
it seems like we are doing
[class.selected]="false"
for all products cuz none of them is selected initially.
How is angular able to call isSelected(myProduct) again and again? How does angular decide when to call isSelected?
angular
angular
asked Aug 20 '16 at 5:26
abdullamabdullam
107212
107212
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
this is an example of parent-child
communication in angular2.
when you are using (click)='clicked(myProduct)'
event, what essentially you are doing is emitting
the value of the selected product using @ouput onProductSelected
property as shown here :
this.onProductSelected.emit(product);
$event` here would be equivalent to `#myProduct.value
when any event gets executed, angular2's change detections kicks-in; checking all the template expression values. It then updates the property binding as per value changes. now everytime value of myProduct
changes, the property binding [class.selected]
need to get updated , hence isSelected
method gets called.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39050767%2fangular-2-click-passing-iteration-variable-and-class-selected-passing-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
this is an example of parent-child
communication in angular2.
when you are using (click)='clicked(myProduct)'
event, what essentially you are doing is emitting
the value of the selected product using @ouput onProductSelected
property as shown here :
this.onProductSelected.emit(product);
$event` here would be equivalent to `#myProduct.value
when any event gets executed, angular2's change detections kicks-in; checking all the template expression values. It then updates the property binding as per value changes. now everytime value of myProduct
changes, the property binding [class.selected]
need to get updated , hence isSelected
method gets called.
add a comment |
this is an example of parent-child
communication in angular2.
when you are using (click)='clicked(myProduct)'
event, what essentially you are doing is emitting
the value of the selected product using @ouput onProductSelected
property as shown here :
this.onProductSelected.emit(product);
$event` here would be equivalent to `#myProduct.value
when any event gets executed, angular2's change detections kicks-in; checking all the template expression values. It then updates the property binding as per value changes. now everytime value of myProduct
changes, the property binding [class.selected]
need to get updated , hence isSelected
method gets called.
add a comment |
this is an example of parent-child
communication in angular2.
when you are using (click)='clicked(myProduct)'
event, what essentially you are doing is emitting
the value of the selected product using @ouput onProductSelected
property as shown here :
this.onProductSelected.emit(product);
$event` here would be equivalent to `#myProduct.value
when any event gets executed, angular2's change detections kicks-in; checking all the template expression values. It then updates the property binding as per value changes. now everytime value of myProduct
changes, the property binding [class.selected]
need to get updated , hence isSelected
method gets called.
this is an example of parent-child
communication in angular2.
when you are using (click)='clicked(myProduct)'
event, what essentially you are doing is emitting
the value of the selected product using @ouput onProductSelected
property as shown here :
this.onProductSelected.emit(product);
$event` here would be equivalent to `#myProduct.value
when any event gets executed, angular2's change detections kicks-in; checking all the template expression values. It then updates the property binding as per value changes. now everytime value of myProduct
changes, the property binding [class.selected]
need to get updated , hence isSelected
method gets called.
edited Mar 17 '17 at 15:33
paqogomez
13.3k44464
13.3k44464
answered Aug 20 '16 at 6:56
candidJcandidJ
3,1911427
3,1911427
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39050767%2fangular-2-click-passing-iteration-variable-and-class-selected-passing-functi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown