ng-repeat won't work when sending form funciton - angularjs

Here is my controller :
.controller('RouteController', function($scope,$timeout, $stateParams, Account) {
$scope.showRouteByCity= function(cityName){
Account.getRouteByCity(cityName).then(function (response) {
$scope.routesByCities = response.data.routeSatart;
console.log($scope.routesByCities);
});
}
}
Here I send array of objects. When i check console, i can see this :
And when i try to use ng-repeat for this object i only get blank screen.
Here is my code from view
<ion-view view-title="All routes">
<ion-content>
<ul>
<li ng-repeat=" tt in routesByCities">{{tt.time}}</li>
</ul>
</ion-content>
</ion-view>
Is there some special method to send variable to view? If I use fucntion or I made some error that i can't see.
Here is what i get in response :
Account.getRouteByCity(cityName) - reponse
SOLUTION 1.
For some reasone, when I use function, it don't want to show me info on view. I found this solution and it works. It's not so clean and nice, so if someone else have any other solution, please, share it.
var cityName = $stateParams.city;
Account.getRouteByCity(cityName).then(function (response) {
$scope.routesByCities = response.data.routeSatart;
console.log($scope.routesByCities);
});
Here I use StateParams to get city name form Url and then i show routes.I don't understand why it don't work with function()...

Try this
<ion-view view-title="All routes">
<ion-content>
<ul ng-repeat=" item in routesByCities">
<li>{{item.time}}</li>
</ul>
</ion-content>
</ion-view>
OR
<ion-content>
<ion-item collection-repeat="item in routesByCities">
{{item.time}}
</ion-item>
</ion-content>

Related

AngularJS and Ionic - changing content based on side menu click in different controller

Ok so I have a situation where I want a click of a side menu item to change the content within the same main view.
Here's a bit of my menu.html:
<ion-item menu-close href="#/app/recipes" ng-click="showFeaturedRecipes()">
Featured Recipes
</ion-item>
<ion-item menu-close href="#/app/recipes" ng-click="showRecipeCategories()">
Recipe Categories
</ion-item>
<ion-item menu-close href="#/app/recipes" ng-click="showAllRecipes()">
All Recipes
</ion-item>
Here's the recipes state (in recipes.html template):
<div class="list card" ng-repeat="recipe in recipes">
...
</div>
And the recipes controller:
.controller('RecipesCtrl', function($scope, $http, $recipes) {
$recipes.getRecipes().then(function(data){
$scope.recipes = data;
});
})
That all works fine, as it uses the $recipes factory to get data from the server and show on page load (i.e. when RecipesCtrl is initiated as it's the state controller)
My issue is that when I click the menu item Featured Recipes, I want the $scope.recipes to change from listing all recipes to just the featured list, but as the function for showFeaturedRecipes() is in the AppCtrl, it doesnt change the same $scope.recipes value:
.controller('AppCtrl', function($scope, $recipes) {
...
$scope.showFeaturedRecipes = function(){
$scope.recipes = $recipes.getFeaturedRecipes();
}
...
})
So essentially at the moment I have a $scope.recipes in the side menu, and one in the actual page content, but they aren't linked.
Any help would be appreciated.
Thanks
The best way is to use different routings for different links. So your menu will be:
<ion-item menu-close href="#/app/recipes?featured=1">
Featured Recipes
</ion-item>
<ion-item menu-close href="#/app/categories">
Recipe Categories
</ion-item>
<ion-item menu-close href="#/app/recipes">
All Recipes
</ion-item>
I believe recipes and categories must have separate view/controllers and featured recipes is just a filter for all recipes.

Model change not getting reflected in view in angularjs

I have got a main index page, in which i initially hide the footer bar. But i want to display it when some item is clicked in some child view. I can see in the logs the model is getting changed, but its not getting reflected in the view.
index page:
<body ng-app="starter">
<ion-pane ng-controller="AudioCtrl">
<ion-nav-bar class="bar-positive">
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<ion-nav-view></ion-nav-view>
<ion-content></ion-content>
<ion-footer-bar ng-hide="musicBar.hide">
<div class="audio-block">
.....
</div>
</ion-footer-bar>
</ion-pane>
</body>
The child view page:
<ion-view ng-controller="AudioCtrl as controller" title="{{selectedCategory}}">
<ion-content>
<ion-list>
<ion-item ng-repeat="item in items" ng-click="$parent.songClicked()">{{item.desc}}</ion-item>
</ion-list>
</ion-content>
</ion-view>
The angular code:
app.controller('AudioCtrl', function ($sce, $scope) {
$scope.musicBar= {
hide: true
};
$scope.songClicked = function(){
console.log($scope.musicBar.hide);
$scope.musicBar.hide = false;
console.log($scope.musicBar.hide);
};
}
);
When i click the item i can see musicBar.hide changing from true to false, but its not reflected in the view. I have also check other similar questions but the solution doesn't work. I have tried using $scope.$digest() and $scope.$apply(), but then i get apply already in progress.
The index.html and the cub view are each managed by their own instance of AudioCtrl, each having their own scope.
The header is made visible when the main view scope's musicBar.hide becomes true, but that never happens, because the subview doesn't modify it. It modifies the sub view scope's musicBar.hide variable.
I don't see any reason why a sub view would have the same controller as the main view. It shouldn't.

Ionic side menu bar not hide properly

I have a ion menu which contain 2 links , Dashboard, favorite.
At the very first time Dashboard page is loaded for application with content. Now when i switch to another menu item. My Content seems populated first after that side menu hides. This causes user experience using the app is bad.
Can we hide side menu first then populated content.
I also tried following steps
<ion-list>
<ion-item menu-close href="#/app/articles" ng-click="toggleLeft()">
Dashboard
</ion-item>
<ion-item menu-close href="#/app/favourite" ng-click="toggleLeft()">
Favorite
</ion-item>
</ion-list>
and
$scope.toggleLeft = function() {
$ionicSideMenuDelegate.toggleLeft();
};
which also doesn't make any effect .
Here is my Dashboard page:-
<ion-view>
<ion-content>
<ion-list can-swipe="true">
<ion-item class="list article-list" ng-repeat="article in articles">
my divs
</ion-item>
</ion-list>
</ion-content>
</ion-view>
and controller is :
.controller('ArticleCtrl', function($scope, $rootScope ,articleService) {
//This is fetching data from database (**Sql lite**)
articleService.fetchArticles().then(function(articles){
$rootScope.articles = articles;
}
});
Similar code for Favorite with different controller.
Any suggestions?
Thanks
This is actually default behaviour but it can be changed. Ionic offers a few events that can be watched and you can than control when to do something. In your case I would populate the $scope only after the view fully enters the screen.
$scope.$on('$ionicView.enter', function (viewInfo, state) {
$scope.data = _getData();
});
So basically the data that you are filling the view with needs to be associated with the $scope only when the view is fully visible.
Edit:
.controller('ArticleCtrl', function($scope, $rootScope ,articleService) {
//This is fetching data from database (**Sql lite**)
var _articles;
articleService.fetchArticles().then(function(articles){
_articles = articles;
}
$scope.$on('$ionicView.enter', function (viewInfo, state) {
$scope.articles = _articles;
});
});

Ionic show select with options on button action

I want to show options while pressing the button not to show dropdown box. For the reference of select. But when I try to implement it gives me dropdown box. I don't want to show it. I want to show options.
Hi This is worked for me based upon your requirement, Please have a look:
1.In your index.html add a button with a method as
<ion-content ng-controller="ExampleCtrl">
<button class="button button-bar button-positive" ng-click="showSelect()">Options</button>
</ion-content>
2.In your app.js file write the controller code with function as:
.controller('ExampleCtrl', ['$scope', '$ionicPopup',function($scope,$ionicPopup) {
$scope.values=['1','2','3','4'];
$scope.showSelect = function(){
$ionicPopup.alert({
title:'select an option',
templateUrl:'test.html'
});
}
}])
3.last and final step create a test.html file in your project and paste the code as:
<ion-view view-title="select">
<ion-content ng-controller="ExampleCtrl">
<ion-list ng-repeat="item in values">
<ion-item>
{{item}}
</ion-item>
</ion-list>
</ion-content>
</ion-view>
Having any queries,Please reply..

Getting empty screen

I am setting the scope with data but all the time getting empty screen.
tried to use some $scope.$apply but it isn't helping.
I can see the scope.items isn't empty...
I can see values only one I click on other tabs.
my code:
.controller('AccountCtrl', function ($scope, $timeout) {
if (localStorage.getItem("itemHistory") !== null) {
$scope.items = localStorage["itemHistory"].split(',');
$scope.$safeApply($scope);
}
});
tab that navigate to template
<!-- Dashboard Tab -->
<ion-tab title="History" icon-off="ion-ios-pulse" icon-on="ion-ios-pulse-strong" href="/tab/account">
<ion-nav-view name="tab-account"></ion-nav-view>
</ion-tab>
my temp:
<ion-nav-bar>
<ion-nav-back-button>
</ion-nav-back-button>
</ion-nav-bar>
<ion-view view-title="Recent Searches">
<ion-content class="padding">
<ul>
<li ng-repeat="item in items">
{{ item }}</li>
</ul>
</ion-content>
</ion-view>
thanks for helping!
I dont see anywhere in your view where the items is being referred. Can you please provide a jsfiddle where I can see the problem? otherwise just looking at this code, I cant make out anything.
And you should not be using explicit apply in this case. apply is a very dangerous call which has to be used with lot of due diligence. It would trigger a digest cycle that trickles up till the rootscope which is what you may NOT want.
I find the issue, it was simple mistake on app.js didnt set the correct view on the app.js

Resources