0

I have a Cordova app written in Angular 2 that allows the user to send links/url between users. When the user for example enters www.google.com and send it to other users we have to make it clickable. And for that I use RegEx, this is what it lookes likes when the url starts with www:

replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = regexpStr.replace(replacePattern2, '<a href="http://$2" 
(click)="openLink2(replacePattern2)" target="_system" class="link-
style">$2</a>');

In the component where the user is going to click on the link the openLink2 function looks like this:

openLink2(url2) {
window.open(url2, '_system', 'location=yes, 
closebuttoncaption=Return');
}

The problem is that when the HTML compiles the (click)="openLink2(replacePattern2)" is gone. Is there anyway that replacePattern2 is removing "(click)" from the string? How can I solve this problem so the user can open the link in systems browser in Cordova??

lin001
  • 33
  • 1
  • 10
  • Using angular, you should parse the message string and wrap it inside a custom component, you should never try to replace text inside your template using regex. The idea would be to get the message, put it inside a LinkMessageComponent, or something like that, which includes a button, redirecting upon click. – Supamiu Dec 12 '17 at 08:16
  • Ok, but how can I find the url in the string and add an a tag and https:// before www so it becomes clickable? This message can include other text excepts just the url @Supamiu – lin001 Dec 12 '17 at 09:00
  • You can use a regex to parse the string, just don't parse the template with it. You can find more details on tese questions: https://stackoverflow.com/questions/37684/how-to-replace-plain-urls-with-links?rq=1, https://stackoverflow.com/questions/39276617/angular2-way-of-converting-plain-text-to-url-anchor-links – Supamiu Dec 12 '17 at 09:14
  • Now I've used this Autolinker.js to change url to clickable links and it works well in broser on my computer but when I run it in Cordova the link is still opening inside the app. I want it to open on the device browser because otherwise the user wont be able to go back to the app when the link is clicked. The target in the HTML string is "_system". How will I get the link to open in the systems browser when the app is running in Cordova? @Supamiu – lin001 Dec 12 '17 at 12:18
  • https://stackoverflow.com/questions/17887348/phonegap-open-link-in-browser Seems like that's the solution. – Supamiu Dec 12 '17 at 12:26

0 Answers0