I am working on an Ionic project with the api from the Movie Database. I have this array in an ng-repeat that shows me some images (cast pictures). But not all the images in the array have an actual picture.
Now I am trying to show a default image when the api does not provide an image. My question is how to do this with ng-if i suppose. In my controller I created a scope as followed:
$scope.default_image = '../img/default_poster.png';
Part of my controller:
.controller('MovieDetailCtrl', function($scope, $stateParams, $ionicLoading, MovieService) {
$ionicLoading.show({template: 'Loading ...'});
$scope.init = function(){
MovieService.getCastCrewInfo($stateParams.id).then(function(data){
$scope.casts = MovieService.castMovie;
$scope.default_image = '../img/default_poster.png';
});
$ionicLoading.hide();
}
})
Part of my HTML code:
<div class="content-cast">
<!-- <h3>Cast</h3> -->
<br>
<hscroller>
<ion-scroll direction="x" scrollbar-x="false" >
<hcard ng-repeat="cast in casts" >
<img ng-src="http://image.tmdb.org/t/p/w154/{{cast.profile_path}} || default_image" >
<p class="cast-name">{{cast.name}}</p>
</hcard>
</ion-scroll>
</hscroller>
</div>
Either use a scope function or ng-if
$scope.getImage = function(item){
return item.profile_path ? 'http://image.tmdb.org/t/p/w154/' + item.profile_path : $scope.default_image;
});
View
<img ng-src="{{getImage(cast)}}" >
For ng-if
<img ng-if="cast.profile_path" ng-src="http://image.tmdb.org/t/p/w154/{{cast.profile_path}}">
<img ng-if="!cast.profile_path" ng-src="{{default_image}}">
Related
I have an ionic & angular based project. I get variable node from a service. node's body value is HTML. But when I print {{node.body.und[0].value}} with ionic, it prints HTML codes with tags like <ul><li><strong>Title:</strong> some text</li><ul>
Controller and service part of the code:
.controller('NodeCtrl', function($scope, $stateParams, Node) {
$scope.node = Node.get({nid: $stateParams.nid});
});
.factory('Node', function ($resource) {
return $resource('some-domain.com/api/node/:nid'+'.json');
})
Ionic template:
<ion-view view-title="{{node.title}}">
<ion-content>
<div class="list card">
<div class="item">
<h2>{{node.title}}</h2>
</div>
<div class="item item-body">
{{node.body.und[0].value}}
</div>
</div>
</ion-content>
</ion-view>
How can I make ionic print {{node.body.und[0].value}} as HTML instead of with HTML tags like <ul><li><strong>Title:</strong> some text</li><ul>?
All Credits to Will.Harris!!
ng-bind-html, is what you are looking for.
From the Docs, use the ng-bind-html in the view as:
<div ng-controller="ExampleController">
<p ng-bind-html="myHTML"></p>
</div>
and in the controller, assign the scope variable with the html content as:
angular.module('bindHtmlExample', ['ngSanitize'])
.controller('ExampleController', ['$scope', function($scope) {
$scope.myHTML =
'I am an <code>HTML</code>string with ' +
'links! and other <em>stuff</em>';
}]);
When the index.html page is rendered my ApplicationController will be called. When I login (login success)I want to refresh my ApplicationController. How to do it?
I have defined below Controllers in my application.
1)Login
angular.module('aclf').controller('LoginController', LoginController);
// The $inject property is an array of service names to inject.
LoginController.$inject = [ '$location', 'AuthenticationService',
'FlashService' ];
function LoginController($location, AuthenticationService, FlashService) {
var loginController = this;
loginController.login = login;
(function initController() {
// reset login status
AuthenticationService.ClearCredentials();
})();
function login() {
loginController.dataLoading = true;
AuthenticationService.Login(loginController.username,
loginController.password, function(response) {
if (response.username === loginController.username) {
console.log(response.authToken);
AuthenticationService.SetCurrentUser(
loginController.username,
response.authToken, true);
$location.path('/home');
} else {
FlashService.Error(response.message);
loginController.dataLoading = false;
}
});
}
;
}
2)ApplicationCotroller
angular.module('aclf').controller('ApplicationController',
function($scope,$rootScope) {
$scope.currentUser = $rootScope.globals.currentUser;
console.log('Inside ApplicationController');
})
3)HomeController- When home controller rendered I want to refresh the portion, since it contains currentUser.
/**
* Home Controller
*/
angular.module('aclf').controller('HomeController', HomeController);
// The $inject property is an array of service names to inject.
HomeController.$inject = [ 'UserService', '$rootScope' ];
function HomeController(UserService, $rootScope) {
var homeController = this;
homeController.user = null;
initController();
function initController() {
loadCurrentUser();
}
function loadCurrentUser() {
UserService.GetByUsername($rootScope.globals.currentUser.username)
.then(function(user) {
homeController.user = user;
});
}
}
4)This is my index.html page
Here I have defined my ApplicationController on body part. This needs to be refreshed at least after login and logout
<body data-ng-controller="ApplicationController">
<!-- TOPBAR START -->
<div id="layout-topbar" data-ng-show="currentUser">
<ul id="top-menu">
<li>
<span class="Fs22 FontRobotoLight">Welcome {{currentUser.username}} </span>
</li>
<li>
Logout
</li>
</ul>
</div>
<!-- TOPBAR END -->
<div id="wrapper">
<div id="wrapperIndent">
<div id="layout-menu-cover" class="Animated05 ps-container">
<div class="ps-scrollbar-x-rail" style="left: 0px; bottom: 3px;">
<div class="ps-scrollbar-x" style="left: 0px; width: 0px;"></div>
</div>
<div class="ps-scrollbar-y-rail" style="top: 0px; right: 3px;">
<div class="ps-scrollbar-y" style="top: 0px; height: 0px;"></div>
</div>
</div>
<div id="layout-portlets-cover">
<div class="Container96 Fnone MarAuto">
<div class="Container100">
<div class="ContainerIndent">
<div class="EmptyBox10"></div>
<div
data-ng-class="{ 'alert': flash, 'alert-success': flash.type === 'success', 'alert-danger': flash.type === 'error' }"
data-ng-if="flash" data-ng-bind="flash.message"></div>
<div data-ng-view></div>
</div>
</div>
<!-- footer -->
<div class="Container100">
<div class="ContainerIndent TexAlCenter Fs14">
Angular | All Rights Reserved.</div>
</div>
</div>
</div>
</div>
</div>
</body>
PS: I am very new to AngularJS
I have tried with reload page stuff. But it was not working.
I removed Application Controller and used
$routeProvider.when('/home', {
controller : 'HomeController',
templateUrl : 'partials/home.html',
controllerAs : 'homeController',
leftNav : 'partials/left-nav-main.html',
topNav: 'partials/top-nav.html'
})
.when('/login', {
controller : 'LoginController',
templateUrl : 'partials/login.html',
controllerAs : 'loginController'
})
Here the templateUrl property in the route definition references the view template that is loaded in the ng-view directive of the div element. I tried to simulate something similar to what Angular does for our left
and top navigation.
The value of the topNav property is used to load the top navigation view in the topnav div element ("id = top-nav") using the ng-include directive. We do the same for left navigation too. The ng-if directive in the left-nav section is used to hide left navigation if the current route configuration does not define the leftNav property.
The last part of this integration is setting up the currentRoute property and
binding it to ng-include. Angular sets up the ng-view template using the route configuration templateUrl property, but it does not know or care about the topNav and leftNav properties that we have added. We need to write some custom code that binds the navigation URLs with the respective ng-includes directives.
$scope.$on('$routeChangeSuccess', function (e, current, previous) {
$scope.currentRoute = current;
});
Here is my index.html
<div class="navbar navbar-default navbar-fixed-top top-navbar">
<!--Existing html-->
<div id="top-nav-container" class="second-top-nav">
<div id="top-nav" ng-include="currentRoute.topNav"></div>
</div>
</div>
<div class="container-fluid">
<div id="content-container" class="row">
<div class="col-sm-2 left-nav-bar"
ng-if="currentRoute.leftNav">
<div id="left-nav" ng-include="currentRoute.leftNav"></div>
</div>
<div class="col-sm-10 col-sm-offset-2">
<div id="page-content" ng-view></div>
</div>
</div>
</div>
Now the left-nav and top-nav appears only in home page and does not appear in login page.
Well, I think the best practice is to reload the entire page:
If you are using angular-ui/ui-router
$state.reload();
If you use another router
$route.reload();
1.I was building a ionic Mobile App in that i have a gallery and i was trying to view a image as pop-up from gallery, pop-up was
working fine and it fetch the image path too but unable to see image
on pop-up.
here my app.js
function galleryController($scope, $http, $rootScope, $location,$ionicModal, profile){
/*alert(0);*/
profile.galleries(window.localStorage['token']).success(function(data){
/*alert(JSON.stringify(data));*/
if(data.status == 200)
{
$scope.gallery = data.data.gellary;
//alert(JSON.stringify(data));
}
$ionicModal.fromTemplateUrl('partials/gallery.html', function(modal) {
$scope.gridModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// open video modal
$scope.openModal = function(selected) {
$scope.data = selected;
$scope.gridModal.show();
};
// close video modal
$scope.closeModal = function() {
$scope.gridModal.hide();
};
//Cleanup the video modal when we're done with it!
$scope.$on('$destroy', function() {
$scope.gridModal.remove();
});
});
};
kangaroo.directive('gridImage', function(){
alert("yyyyyyyyyyyyyyyyyyy");
return function($scope, element, attrs){
var url = attrs.gridImage;
element.css({
'background-image': 'url(' + url +')',
});
};
});
I was fetching image from s3 Amazon by using api , this is html
code in my gallery.html where i get the data from my controller i
can see the image in gallery and not able to see on pop-up don't
know what was the problem.
**gallery.html page**
<ion-header class="bar bar-header bar-balanced">
<!-- <button class="button button-icon icon ion-navicon"></button> -->
</ion-header>
<div class="padding" >
<div ng-controller="galleryController">
<ion-content class="content has-header ionic-pseudo" style="margin-top:19px">
<div ng-repeat="x in gallery" >
<img ng-src="{{x.thumb_img}}" ng-click="openModal(x)" alt="" class="gallery_i" />
</div>
</ion-content>
<script id="partials/gallery.html" type="text/ng-template">
<div class="modal" ng-click="closeModal()">
<img ng-src="{{data.thumb_img}}" />
</div>
</script>
</div>
</div>
please help me out of this
Thanks in advance.
Please see this jsFiddle
Within this fiddle I have two html divs that contain Ad Placements. I want to toggle them on/off using Angular so on one instance Ad 1 will show and in another instance Ad 2 will show instead. Note that in each div I have Javascript content that I want to also toggle on/off.
I essentially want to update the View.
HTML:
<div ng-controller="MyCntrl">
<div id = "ad1" class = "ad1">
AD 1
<img src ="http://images.dailytech.com/frontpage/fp__Apple-100x100.png"></img>
<script><!-- code here --></</script>
</div>
<div id = "ad2" class = "ad2">
AD 2
<img src ="http://fixmypod.ca/image/cache/data/NEWEST_Phones/Samsung_Galaxy_Note_2-100x100.jpg"></img>
<script><!-- code here --></</script>
</div>
</div>
Angular:
var app = angular.module('HelloApp', []);
app.controller('MyCtrl',['$scope','$element', function($scope, $element) {
$scope.changeView = function(ad){
//make ad x show and the other ad hidden
}
}]);
How can I go on doing this?
Simply use ng-show instead of a class and use a boolean property on your scope to trigger your divs.
HTML:
<div ng-controller="MyCntrl">
<div id="ad1" ng-show="toggle">
AD 1
<img src ="http://images.dailytech.com/frontpage/fp__Apple-100x100.png"></img>
<script><!-- code here --></</script>
</div>
<div id="ad2" ng-show="!toggle">
AD 2
<img src ="http://fixmypod.ca/image/cache/data/NEWEST_Phones/Samsung_Galaxy_Note_2-100x100.jpg"></img>
<script><!-- code here --></</script>
</div>
</div>
Angular:
var app = angular.module('HelloApp', []);
app.controller('MyCtrl',['$scope','$element', function($scope, $element) {
$scope.toggle = true;
$scope.changeView = function(ad){
$scope.toggle = !$scope.toggle;
}
}]);
Alternatively, you can use ng-include to only render a template based on a property.
HTML:
<div ng-controller="MyCntrl">
<div ng-if="condition">
<div ng-include="`template/path/ad1`"></div>
</div>
<div ng-if="!condition">
<div ng-include="`template/path/ad2`"></div>
</div>
</div>
You can do it by
ngShow
ngHide
ngIf
controller
$scope.myDiv=true;
view
<div ng-show="myDiv">
if true this will visible
</div>
i need to use a ionic slide box to show some images in my application. I used ionic slide box it does not seem to work with a ng-repeat.
this is my html part
<ion-view title="Promotions" ng-controller="PromotionsCtrl">
<ion-content class="has-header" scroll="true" padding="true">
<ion-slide-box>
<ion-slide ng-repeat="obj in promotions">
<img ng-src= {{obj.img}}>
</ion-slide>
</ion-slide-box>
</ion-content>
</ion-view>
my controller
.controller('PromotionsCtrl', function($scope, $http, $window, $ionicSlideBoxDelegate,$interval) {
$http.get( 'http://******.com/B*****/service2.php?q=promotions', { cache: true})
.then(function(res){
$scope.promotions = res.data['top'];
$ionicSlideBoxDelegate.update();
});
})
You have to use update() if you are using ng-repeat with slideboxes.
See here:
http://ionicframework.com/docs/api/service/%24ionicSlideBoxDelegate/
The newer version .rc4 of ionic updates the slide box on its own. Try updating your Ionic Lib.
This is my sample,the slide works,but when the delegate-handler updated,the parameter of the "does-contunue" can't work,I'm Still looking for a solution...
html:
<ion-slide-box on-slide-changed="slideHasChanged($index)" does-continue="true" auto-play="true" delegate-handle="image-viewer" style="height:30%;">
<ion-slide ng-repeat="slide in slideList">
<div class="box" style="background-image: url('{{slide.url}}');background-size:100% 100%;></div>
</ion-slide-box>
controller:
angular.module("myApp.controllers", ["myApp.services"])
.controller("myController", function ($scope, $ionicSlideBoxDelegate, myService,$timeout) {
$timeout(function(){
var slideList = myService.getSlides();
$scope.slideList = slideList;
$ionicSlideBoxDelegate.$getByHandle('image-viewer').update();
},2000)
})
service:
angular.module("myApp.services", [])
.factory("myService", function () {
return{
getSlides: function () {
var slideList = new Array();
slideList.push("imgs/a.jpg");
slideList.push("imgs/b.jpg");
slideList.push("imgs/c.jpg");
return slideList;
}
}
})
As #Karan said, the update is call by itself. But there is still some problems. As mentioned in the issue, you can disable the slider if the data is not ready. Then everything will be fine.