0

Here is my example:

<img data-placement="bottom" data-toggle="tooltip" class="tip"
     data-original-title="{{user.dbInfo.city}}, {{user.dbInfo.country_name}}"
     src="https://countryflags.io/{{user.dbInfo.country}}/flat/24.png">

and I want to show the {{user.dbInfo.city}}, only if the variable exists. In some cases there is no city.

How can I achieve this ?

4
  • Have you already tried *ngIf ? Commented Dec 5, 2019 at 17:08
  • In this case it is better to build the whole string in component and then pass it to data-original-title, because template solution might be weird Commented Dec 5, 2019 at 17:13
  • Did you try user.dbInfo.hasOwnProperty('city') in ngIf ?? Commented Dec 5, 2019 at 17:15
  • have you tried *ngIf=" user.dbInfo.city != ',' " Commented Dec 5, 2019 at 17:17

5 Answers 5

1

You can do it as follows.

 <img data-placement="bottom" data-toggle="tooltip" class="tip"
    data-original-title="{{ user.dbInfo.city ? user.dbInfo.city + ',' + user.dbInfo.country_name : user.dbInfo.country_name }}"
    src="https://countryflags.io/{{user.dbInfo.country}}/flat/24.png">
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error http://errors.angularjs.org/1.5.8/$parse/ueoe?p0=user.dbInfo.city%20%3F%20user.dbInfo.city%20%2B%20'%2C'%20%2B%20user.dbInfo.country_name%20%3F%20user.dbInfo.country_name
sorry small mistake i have added extra ?. can you check now ?
0

From what I understand you want to display another data-original-title based on the existence of the variable.

<ng-container *ngIf="user.dbInfo.city">
    <img data-placement="bottom" data-toggle="tooltip" class="tip" data-original-title="{{user.dbInfo.city}}, {{user.dbInfo.country_name}}" src="https://countryflags.io/{{user.dbInfo.country}}/flat/24.png">
</ng-container>
<ng-container *ngIf="!user.dbInfo.city">
    <img data-placement="bottom" data-toggle="tooltip" class="tip" data-original-title="{{user.dbInfo.country_name}}" src="https://countryflags.io/{{user.dbInfo.country}}/flat/24.png">
</ng-container>

3 Comments

Hello, I am using Angular with ng-if version
too complicated for such small problem
@LuninRoman If you have a better way to do it, feel free to propose your own answer. That is a way to achieve what is asked.
0

For things like this, I always create a method in my component class, something like formatImageOriginalTitle(dbInfo) which would look like this:

formatImageOriginalTitle(dbInfo) {
    return dbInfo.city ? `${dbInfo.city}, ${dbInfo.country_name}` : dbInfo.country_name;
}

Then in the markup:

<img ... data-original-title="formatImageOriginalTitle(user.dbInfo)" ... />

If this is something you may reuse in other places, you may create a pipe instead.

1 Comment

Here's a working example: stackblitz.com/edit/angular-aqnmba
0

You can evaluate it in the template itself by using property binding.

[attr.original-title]="(user.dbInfo.city ? user.dbInfo.city + ',' : ' ') + user.dbInfo.country_name"

Try like this:

<img data-placement="bottom" data-toggle="tooltip" class="tip"
 [attr.original-title]="(user.dbInfo.city ? user.dbInfo.city + ',' : '') + user.dbInfo.country_name"
 src="https://countryflags.io/{{user.dbInfo.country}}/flat/24.png">

Working Demo

6 Comments

Hello, for this data-original-title="{{user.dbInfo.city}} + ({{user.dbInfo.city}} ? ',' : '') + {{user.dbInfo.country_name}}" I get this result Athens + (Athens ? ',' : '') + Greece
But I need it to be like Athens, Greece
Use property binding
I don't know what property binding is
[attr.original-title]="" this is property binding. {{}} is interpolation. Try [attr.original-title]="(user.dbInfo.city ? user.dbInfo.city + ',' : ' ') + user.dbInfo.country_name"
|
0

A function as @MattU said can help you

var app = angular.module("app", []);

app.controller("MainCtrl", function($scope) {
  $scope.user1 = {
    dbInfo: {
      city: 'CITY',
      country: 'be'
    }
  };

  $scope.user2 = {
    dbInfo: {
      country: 'be'
    }
  };


  $scope.getTitle = function(obj) {
    if (obj.dbInfo.hasOwnProperty('city')) {
      return obj.dbInfo.city + ',' + obj.dbInfo.country;
    }
    return obj.dbInfo.country;
  };

});
<html ng-app="app">

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.0/angular.min.js"></script>
</head>

<body ng-controller="MainCtrl">
  <img data-placement="bottom" data-toggle="tooltip" class="tip" data-original-title="{{getTitle(user1)}}" src="https://www.countryflags.io/{{user1.dbInfo.country}}/flat/24.png">

  <img data-placement="bottom" data-toggle="tooltip" class="tip" data-original-title="{{getTitle(user2)}}" src="https://www.countryflags.io/{{user2.dbInfo.country}}/flat/24.png">
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.