0

A simple ng-bind like this

<div ng-bind="author.Title.Value + ' ' + author.FirstName.Value + ' ' + 
author.LastName.Value"></div>

results in

Dr. null Franklin

if author.FirstName.Value is null.

Is there a simple way to replace null by an empty string (I know I can concatenate this string in the controller and return something like FullName, but I'd prefer to do it this way).

4 Answers 4

2

you don't need to add code in controller by this way

<div ng-bind="(author.Title.Value||"") + ' ' + (author.FirstName.Value||"") + ' ' 
+ (author.LastName.Value||"")"></div>
Sign up to request clarification or add additional context in comments.

Comments

1

try this

<div ng-bind="author.Title.Value + ' ' + author + ' ' + 
author.LastName.Value"></div>

while in the js you put this

if(author.FirstName.Value==null){
    $scope.author='';
}else{
    $scope.author=author.FirstName.Value
}

2 Comments

Thanks - but as stated in my comment to callmekatootie, I wanted to avoid additional controller code.
you can put it in the view with the script tag, essentially i don't think you can controll that thing in a way that you don't have to write something in the script.
1

The "simplest" way to do this is to move the logic for concatenating your string into the controller. It has the following advantages:

a) It separates the view logic from the controller logic
b) It is easier to read and understand
c) It solves your problem

So, you can have a function in your controller such as:

$scope.getAuthorFullName = function (author) {
    var returnValue = '';

    if (author.Title.Value) {
        returnValue += author.Title.Value + ' ';
    }

    if (author.FirstName.Value) {
        returnValue += author.FirstName.Value + ' ';
    }

    if (author.LastName.Value) {
        returnValue += author.LastName.Value;
    }

    return returnValue;
};

and in your view, you can have:

<div ng-bind="getAuthorFullName(author)></div>

3 Comments

Thanks, and sure - a simple controller function achieves this easily, but I was hoping for something more simple because there really isn't much controller logic in this, it is easy to understand both ways, and if you have a lot of concatenated strings like this, you need to write a lot of boring controller code.
You will find that its really useful to follow this practice when working with AngularJS. By the time you realize, your application would have grown and the view would be polluted with controller logic. Don't hesitate to have such a setup.
I've been working with AngularJS a long time now, and I really know my way around, so using a controller function for this would be a natural thing. All I wanted to know if there is another way to concatenate strings when binding.
1

You could also use something like this:

<input type="text" value="{{ surname == 'null' ? '' : surname  }}"  />

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.