Shifat
Shifat

Reputation: 762

Concating text in ng-model

In my view :

<input type="text"  ng-model="imageUrl = fullImageUrl(job.imageUrl);" />

My application controller :

    $scope.fullImageUrl=function(imageUrl){

    return '/img/'+imageUrl;
}

Problem is that , when i browse it shows -

/img/undefined

I don't want this. I want blank , before executing my function.

Upvotes: 2

Views: 39

Answers (2)

Shifat
Shifat

Reputation: 762

$scope.fullImageUrl=function(imageUrl){
    if (!imageUrl) {            
        return "";
        }

    return '/img/'+imageUrl;
}

Here i am passing imageUrl as parameter and at first it is undefined. Thats why when !imageUrl , it will return empty string and show it on view . As soon as it gets the value , it will not enter into "if- condition" and return the given code.

Upvotes: 0

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23532

It's probably not the best idea to use an assignment in ng-model. It expects an expression, not an assignment. This is how it should look:

<input type="text" ng-model="fullImageUrl(job.imageUrl)" />

Then if you want it not to show undefined you can just check it in your function and assign an empty string "" to it. Try something like this:

$scope.fullImageUrl=function(imageUrl){
    if (!imageUrl) imageUrl = "";
    return '/img/'+imageUrl;
}

Upvotes: 2

Related Questions