Given this array,i want when i click on a demo it will take me to a link if i click another demo it will take me to another link:
angular.module('app', [])
.controller('MainCtrl', function($scope) {
$scope.link = function(){
var demo = $scope.demos.filter(d => d.paragrafo === 'request')
if(demo){
$scope.link = 'https://www.google.it'
}else{
alert('Hello')
}
}
$scope.demos = [ {
paragrafo: 'request',
title:'demo1',
paragrafo2:'dskjdfdjfdfjkdf',
id: 1
},
{
paragrafo: 'params',
title:'demo2',
paragrafo2:'dfhfhfjgfkjghfjkgf',
id: 2
},
{
paragrafo: 'request',
title:'demo3',
paragrafo2:'sjdsdjddfjdf',
id: 3
},
{
paragrafo: 'params',
title:'demo4',
paragrafo2:'sdjkdhdkjfhdjfh',
id: 4
},
];
})
I tried to do if condition but i don't know how to do
this is my component, where i put my link:
.directive('card', function(){
var TPL = `<div class="card" style="width: 18rem">
<div class="card-body">
<p>{{c.paragrafo}}</p>
<h4 class="card-title">{{c.title}}</h4>
<h6 class="card-subtitle mb-2 text-muted">{{c.paragrafo2}}</h6>
<p class="card-text"></p>
<a href ng-click="c.link()" class="card-link">something</a>
</div>
</div>`
var directive = {
restrict: 'E',
template: TPL,
scope: {
paragrafo: '@',
title: '@',
paragrafo2: '@',
link: '&'
},
controller: ctrlFn,
controllerAs: 'c',
bindToController: true
};
now i have an alert, but i would like to generate a different link for each demo
the Array filter method returns an array. Just set the link property in each filtered array item:
$scope.link = function(){
var filteredDemos = $scope.demos.filter(d => d.paragrafo === 'request')
for(var i=0; i<filteredDemos.length; i++) {
filteredDemos.link = 'https://www.google.it';
}
}