Creating clickable phone numbers in JSON elements for browser display to enable direct calling action

There is a Phone.json file with the following contents:

   {
        "Call": "Hi, Please contact the custom care at (119)239-9999 for further discussions"
    },
    {
        "Call": " For emergency dial 911 as soon as possible"
    }

The goal is to display the above strings on a mobile browser and enable users to click to call using the tel protocol

Here is the implemented approach:

Index.html:

<body ng-controller="PhoneListCtrl">

    <ul  ng-repeat="phone in phones">
        <li>
         <a ng-click="convertContactNumbers(phone.Call)">{{phone.Call}}</li>
    </ul>
</body>

Controller:

myApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http)
    {
        $http.get('Phone.json').
                success(function(data)
                {

                    $scope.phones = data;

                });
                $scope.convertContactNumbers = function(st)
                {
            if (st.match((/(((\(?\+?1?\)?)\s?-?)?((\(?\d{3}\)?\s?-?\s?))?\s?\d{3}\s?-?\s?\d{4}){1}/gm)))
            {
                st = st.replace(/(((\(?\+?1?\)?)\s?-?)?((\(?\d{3}\)?\s?-?\s?))?\s?\d{3}\s?-?\s?\d{4}){1}/gm, "<a href='tel:$1'>$1</a>");

                return(st);
            }
            else
            {
                st = st.replace(/([0-9]{3})/, "<a href='tel:$1'>$1</a>");

                return(st);
            }

                }
    }]);

The issue is in handling the st variable which contains

"For emergency dial <a href='tel:911'>911</a> as soon as possible"
, so that it can be rendered as an HTML page to make the links clickable. How can this be achieved?

Answer №1

Check out this query: How can $sce.trustAsHtml(string) be used to replicate ng-bind-html-unsafe in Angular 1.2+

This instructs Angular to treat the string as HTML.

myApp.filter('unsafe', function($sce) {
    return function(val) {
        return $sce.trustAsHtml(val);
    };
});

Here's the corresponding view:

<ul  ng-repeat="phone in phones">
    <li  ng-bind-html="convertContactNumbers(phone.Call) | unsafe"></li>
</ul>

Example of it working: http://plnkr.co/edit/eV04J9gjjzJ8h3IGM4cZ?p=info

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

AngularJS - Displaying the initial content in the <ui-view> container

Can default code be placed inside a <ui-view> element without any issues? It seems to work for me, but I can't find any definitive information on whether it's acceptable or not. Currently, I have set up the "default" view to display a list ...

What is the best way to eliminate null value Strings from JSON when converting a POJO to JSON using the Json.toJson() method in the Play framework

Is there a way to obtain JSON result without any null values when converting from POJO to JSON? class Test{ public String id; public String firstname; public String lastname; } Test test=new Test(); test.i ...

Only line breaks are permitted in Mustache.js, all other HTML characters are escaped

After a user submits input, I am generating comments and displaying them using Mustache.js. When it comes to rendering the comments, I have found that replacing user-entered line breaks (\n) with <br/> allows them to display as HTML breaks. To ...

Ways to retrieve a grandchild element of a specific element using its tag name

I'm struggling to access elements that are nested within various levels of parent elements with and without IDs. The usual method getElementByTagName doesn't seem to be working for me, especially when the target element is three levels deep. Is t ...

Is Jackson a suitable tool for conducting XSLT transformations?

Within our projects, we utilize Jackson for mapping between JSON and Java objects, as well as Jettison for converting XML input streams to JSON objects. One common scenario involves applying an XSLT transformation on an XML document to create a "JSONized" ...

Are you struggling to differentiate between a JSON array and a JSON object?

{ error: false -booking: [2] -0: { booking_id: 32 booking_user_id: 25 booking_service_id: 1 booking_date: "2015-10-01 12:16:48" booking_completion_date: "0000-00-00 00:00:00" booking_ ...

Display the element following a specific component while looping through an array in Vue.js

Currently, I am facing an issue while using a for-loop on the component element. My goal is to display a <p> element next to the <component> element during the loop's third iteration. The challenge lies in accessing the iteration variable ...

Sinon - using callbacks in stubbed functions leading to test method exceeding time limit

One of my express route methods is structured as follows: exports.register_post = function(req, res) { var account = new Account(); account.firstName = req.param('firstName'); //etc... account.save(function(err, result) { ...

Securing data with ngModel encryption in AngularJS

Is it possible to encrypt only the $modelValue of a ngModel using any algorithm, while keeping the view value in plain text? I attempted to create a custom directive to achieve this: angular.module('utilityModule').directive('encrypt' ...

Issues with executing Unit Tests in Karma using Webpack and Jasmine

Trying to set up testing with Karma, Jasmine, and Webpack in AngularJS has been quite a journey for me. While my webpack is functioning smoothly and I can run my website using npm start, things take a turn when I attempt to include my app.js. Despite nume ...

When executing an $http get request in Angular, a promise is

After implementing this code in my service and noticing that it works, I have a question. Since $http.get() returns a promise which executes asynchronously, I am confused about why I need to use deffered.resolve(res.data) to return data in my service. Yo ...

Steps to develop a runnable Express API

After developing a basic yet fully functional Node.js Express API application using JavaScript, I am now looking to convert it into an executable file that can be run on Windows systems. The main reason behind this is to provide clients with the option of ...

Issue with scrolling to the bottom of collapsed sections in Bootstrap

I have a bootstrap collapse panel and I've added a toggle link at the bottom to allow users to expand and collapse the content with a click. The Issue My problem arises when the menu expands, causing it to scroll all the way to the bottom of the pag ...

Discover which npm module includes the lodash dependency

I've encountered a peculiar situation while using webpack to create a production bundle for my application. Even though I haven't explicitly installed `lodash` and it's not listed in my package.json file, I noticed that it's being added ...

Troublesome GSP: JavaScript not functioning properly in Gr

I am experiencing an issue with the JavaScript code I have. Here's my code: <script type="text/javascript"><!-- ​$(function () { $('#add').click(function() { $(this).before($('select:eq(0)').clone()); ...

Why do I keep getting errors in TypeScript when I manipulate DOM elements using getElementsByClassName(), even though my application still functions properly?

I am dealing with an Angular2 application. Unfortunately, I have had to resort to using the following code within a component method (I know it's not ideal, but...): let confirmWindowDOM = document.getElementsByClassName('modal')[0]; confir ...

What is the best way to utilize the features of component A within component B when they exist as separate entities

Component A has all the necessary functionalities, and I want to use it in Component B. The code for ComponentA.ts is extensive, but it's not written in a service. How can I utilize the logic from Component A without using a service, considering both ...

The subsequent menu selection will be based on the chosen menu value

I am attempting to accomplish the following: https://i.sstatic.net/JffUWC02.png Essentially, I need a select options menu with labels where selecting an option will display a corresponding value. These values should then become labels for a second selec ...

Tips for getting Vue's element-ui validateField() function to function correctly in conjunction with v-if?

Kindly observe the password fields. The fields for 'Password' and 'Confirm Password' are displayed upon clicking on the 'Change Password?' button. The provided code functions properly and effectively validates the form using ...

Using Ruby on Rails: A guide to creating a custom rake task for analyzing Twitter JSON data

I am currently in the process of developing a rake task to analyze a file that contains numerous Twitter updates in JSON format. The JSON file has the following structure: { "_id" : { "$oid" : "50f82aeab4aa879861000000" }, "text" : "Golden Globes, lots o ...