How to remove slash from image ng-src url? - angularjs

I get an error 400 from the images server provider because the request include slash in the end of the image url
<div class="footer accordian" ng-hide="photosSlider">
<ul>
<li ng-repeat="item in items">
<div class="image_title">
KungFu Panda
</div>
<a href="#">
<img ng-src={{item.img}}/>
</a>
</li>
</ul>
</div>
The items array, holds images url without slash in the tail of the url
When Angular send request for the image somehow slash added to the url
Any idea why the slash added and how to remove?

You can just remove the / from inside the ng-repeat itself.
Use this
<img ng-src={{item.img.substring(0, item.img.length - 1)}}/>

Related

Place Photo Requests with google api in img src

I am trying to get list of location with their photos using google places api.
I am storing the response in a variable vm.locationList. In my view I am iterating over this variable to show location list.
Following is the code
<div ng-repeat = "(key, value) in vm.locationList">
<div class="panel panel-primary">
<div class="panel-body">
<div class="col-md-2">
<div class="pull-left thumbnail">
<span ng-if="vm.getPhotos">
<img src="https://maps.googleapis.com/maps/api/place/photo?maxwidth=150&photoreference={{value.photos[0].photo_reference}}&key=API_KEY" class="img-responsive" style="margin-right:10px;">
</span>
</div>
</div>
<div class="col-md-10">
<h4><b>{{value.name}}</b></h4>
<p><i class="fa fa-map-marker" style="color:red"></i> {{value.vicinity}}</p>
<p> open now: {{value.opening_hours.open_now}}</p>
</div>
<div ng-repeat = "type in value.types">
<p style="float:left;width:20%" class="tags">{{type}}<p>
</div>
</div>
</div>
Now to get the photo I place http url request in src attribute of img tag. Though I am getting pics with the list but I see error in console saying bad request.
Following is the error I am getting in console
GET https://maps.googleapis.com/maps/api/place/photo?maxwidth=150&photoreference={{value.photos[0].photo_reference}}&key=API_KEY 400 ()
And here is the http response
<title>Error 400 (Bad Request)!!1</title>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
Okay I have got where I was doing mistake. I used Angular's ng-src in place of src attribute. Now its working fine. Binding was not working earlier. I got confused because even though binding was not working image was appearing.

why my image root not found

i am using go-lang with angular to build a car view in this approach:
this is my main.go
router.GET("/shopping", carBaseController.CarsViewIndex)
router.GET("/images", galleryBaseController.GetImages)
in my controller\cars.go
func (controller CarController) CarsViewIndex(c *gin.Context) {
c.HTML(http.StatusOK, "carview.html", gin.H{
"title": "Car Page"})
}
func (controller GalleryController) GetImages(c *gin.Context) {
imageList := controller.galleryService.FindImages(&bson.M{})
c.JSON(http.StatusOK, &imageList)
fmt.Println(imageList)
}
in my carview.html
<div class="row mix-grid" ng-init="GetAllImages()" >
<div class="col-md-3 mix photography" ng-repeat="image in allimages">
<div class="hover-effect" >
<div class="img" ><img src="imagesT/gallery/Thumb/{{image.imagename}}" alt=""
class="img-responsive"/></div>
<div class="info"><h3>Manage Images</h3><a href="#"
class="mix-link"><i
class="glyphicon glyphicon-link"></i></a><a
href="imagesT/gallery/{{image.imagename}}" data-lightbox="image-1"
data-title="Image 1" class="mix-zoom"><i
class="glyphicon glyphicon-search"></i></a></div>
</div>
this is the site fo cars
<a class="btn btn-default" href="carinfo">Learn More
<span class="glyphicon glyphicon-chevron-right"></span></a>
</div>
</div>
when i enter http://localhost:3030/shopping every thing work fine but i found this error
"NetworkError: 404 Not Found -
http://localhost:3030/imagesT/gallery/Thumb/%7B%7Bimage.imagename%7D%7D"
i do not know why this error happen while every thing work fine and this what i fount with firbug :
<img class="img-responsive" alt="" src="imagesT/gallery/Thumb/2016-chevrolet-cruze-spied-completely-uncovered-news-car-and-driver-photo-658949-s-217x132.jpg">
Have you tried with ng-src instead of src ?
If you use ng-src instead of src in your img tag, you can resolve this issue.
Using Angular markup like {{hash}} in a src attribute doesn't work
right: The browser will fetch from the URL with the literal text
{{hash}} until Angular replaces the expression inside {{hash}}. The
ngSrc directive solves this problem.
Check out the docs here.

Can't open link with dynamic content on Ionic

I have the HTML below on my Ionic app:
<a ng-href="#/app/perfil/{{ notification.Notification.model_id }}">
<img ng-src="{{ notification.Notification.photo }}" class="profile-pic">
<h2 ng-bind-html="notification.Notification.name">{{ notification.Notification.name }}</h2>
<p ng-bind-html="notification.Notification.title">{{ notification.Notification.title }}</p>
</a>
As you can see, it set the user ID after loading a JSON file. This link works fine on a browser, but for some reason when I try on a real device, my xcode returns an error:
Failed to load webpage with error: The URL can’t be shown
All my data is being served on a HTTPs URL.
Any idea on how to fix that?
After some research, I've changed my ng-href to ui-sref and it worked - actually, ui-sref makes much more sense in this case.
If any one is having the same issue, please read more details here: https://github.com/angular-ui/ui-router/wiki/Quick-Reference#ui-sref
My code:
<a ui-sref="appPerfil.perfil-amigo({ user_id: notification.Notification.model_id })">
<img ng-src="{{ notification.Notification.photo }}" class="profile-pic">
<h2 ng-bind-html="notification.Notification.name">{{ notification.Notification.name }}</h2>
<p ng-bind-html="notification.Notification.title">{{ notification.Notification.title }}</p>
</a>

ng-repeat inserting empty anchor tags

I'm trying to create a menu using angular. A menu item can have children requiring another ng-repeat to print the sub nav items. I'm noticing some strange behavior when attempting to insert an anchor tag within the 2nd ng-repeat.
Link to fiddle: http://jsfiddle.net/npU7t/
<li ng-repeat="sub_menu_item in menu_item.sub_menu">
<a href="">
{{ sub_menu_item.title }}
</a>
</li>
With
{
title: 'menu item with children',
sub_menu: [
{
title: '<-- empty anchor tag???'
}
]
}
Results in
<li ng-repeat="sub_menu_item in menu_item.sub_menu" class="ng-scope">
<-- empty anchor tag???
</li>
Where the did duplicate / empty anchor tag come from? How can I prevent it from being created?
Appreciate the help!
This isn't a bug with Angular, but rather how you have your markup.
UPDATE:
The issue is actually the nested <a> tag, not the <ul> tag.
<a href="">
<span class="title">{{ menu_item.title }}</span>
<ul class="sub-menu" ng-if="menu_item.sub_menu">
<li ng-repeat="sub_menu_item in menu_item.sub_menu">
<a href="">
{{ sub_menu_item.title }}
</a>
</li>
</ul>
</a>
In fact, if you remove Angular from the equation altogether, you will see that the extraneous <a> tag is still added to the DOM: http://jsfiddle.net/jwcarroll/cXkj4/
If you get rid of the nested <a> tag, then the extra element will disappear.
<a href="">
<span class="title">{{ menu_item.title }}</span>
</a>
<ul class="sub-menu" ng-if="menu_item.sub_menu">
<li ng-repeat="sub_menu_item in menu_item.sub_menu">
<a href="">
{{ sub_menu_item.title }}
</a>
</li>
</ul>
In both HTML 4.01, and HTML 5, having a nested <a> tag is a no no.
The simplest possible recreation of the problem I could come up with is this bit of markup:
<a href="">Outer
<p>Blah
Inner
</p>
</a>
Because you can't nest <a> elements within each other, the browser is doing it's best to recreate your intent while keeping the DOM clean. What you end up with is this:
Outer
<p>
Blah
Inner
</p>
This makes sense when you realize what the browser is trying to do. The browser is trying to do three things:
Keep Outer, Blah and Inner text elements inside hyperlinks
Contain Blah and <a>Inner</a> inside a single <p> tag
Ensure no <a> tags are nested within each other
The only sensible way to accomplish all three of these is to wrap both Outer and Blah text elements in separate <a> tags in a way that isn't nested. This is the closest approximation to the original intent without breaking the DOCTYPE rules.
I hope this helps.
Very strange. It doesn't appear with any tag besides <a> (like <p> or <div>). It looks like an outright bug to me - I'd submit a proper bug report.

How to use AngularJs Inside the Scala template?

I'm trying to show the image from the database. However, I can't put the angular variable inside the # sign of the Scala template.
<div class="col-sm-6 col-md-3" ng-repeat="product in products">
<a href="#">
<div class="thumbnail">
<img class="img-responsive" ng-src="#routes.BookStore.getImage(product.name)">
...
</div>
</a>
</div>
It gave Error: Can't find the product variable. I also tried:
<img class="img-responsive" ng-src="#routes.BookStore.getImage( {{ product.name }} )">
It still gave me the same error. How can I use the AngularJs variable inside the Scala template?
You CAN NOT that's obvious - Scala template is processed at backend side much, much earlier then it arrives to frontend. Instead your Angular app should have some method which will create a string containing path to the image literally, something like /book-store/get-image/foo.jpg and then add a route to your routes file:
GET /book-store/get-image/:fileName controllers.BookStore.getImage(fileName)
Optionally you can try to go with javascriptRoutes, but it's not necessary.

Resources