2

I have this markup in my angularJS mark up

<div class="photo" style="background-image: url('{{item.mageURL}}')"></div>

The {{item.mageURL}} expression producing a string like:

http://example.com/photos/68678/stamp

I would like to change it to:

http://example.com/photos/68678

So far, without success, I've tried...

<div class="photo" style="background-image: url('{{item.mageURL.str.substring( 0, str.indexOf( 'stamp' ) )}}')"></div>

But it just returns an empty string.

Any idea how I can do this in AngularJS

2
  • 1
    I would suggest you to keep this kind of logic in your controller, and keep templates easier to read. Cheers. Commented Jan 11, 2017 at 19:32
  • I would suggest you not dirty up your controllers and instead either fix what your issue is (which it looks like you have a few solutions regarding the String APIs) OR utilize filters instead. Commented Jan 11, 2017 at 19:37

3 Answers 3

2

You have to apply substring method to item.mageURL.str string.

'{{item.mageURL.str.substring( 0, item.mageURL.str.indexOf( '/stamp' ) )}}'

Another method is to use split method.

<div class="photo" style="background-image: url('{{item.mageURL.str.split('/stamp')[0]}}')"></div>

function MyCtrl($scope) {
  $scope.item={
      "mageURL":{
            "str":'http://example.com/photos/68678/stamp'
        }
  };
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
  <div ng-controller="MyCtrl">
    <div class="photo" style="background-image: url('{{item.mageURL}}')"></div>
    {{item.mageURL.str.split('/stamp')[0]}}<br>
    {{item.mageURL.str.substring( 0, item.mageURL.str.indexOf( '/stamp' ) )}}
  </div>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

I like this best, if it works. Keep your controllers simple and UX driven. Keep logic like this either in your templates or use a filter.
ahh, that makes sense. Damn copy & past with str
1

please use 'ng-style' https://docs.angularjs.org/api/ng/directive/ngStyle

ng-style="{'background-image' : 'url(' + item.mageURL.str.substring( 0, item.mageURL.str.indexOf( 'stamp' ) )+')'}"

<div class="photo" ng-style="{'background-image' : 'url(' +  item.mageURL.str.substring( 0, item.mageURL.str.indexOf( 'stamp' ) )+')'}"></div>

4 Comments

why is ng-style better in this case?
in general performance is better with ng-style as it adds a watch on the model and will update the view only if the model has changed, in this case item.mageurl.str. When you use style it will execute everytime.
interesting, it is wrapped in a ng-repeat so I think it has a watch on it already?
the ng-repeat watch triggers when count of the array / properties changes. ng-style has its own watch.
0

I think it'll be much better to create a tiny directive for this purpose, like this

app.directive('bgImg', function() {
  return function(scope, elem, attrs) {
    elem.css('background-image', someTransformFn(attrs.bgImg));
  }
})

Long JS-expressions in the templates are the real pain, in comparison with this:

<div bg-img="{{vm.someImg}}">

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.