Skip to content

Commit 07e4acf

Browse files
perform mehtods from view lifecycle
1 parent e2ccd0d commit 07e4acf

File tree

4 files changed

+94
-31
lines changed

4 files changed

+94
-31
lines changed

readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#view lifecycle
2+
3+
i created a directive to show the view lifecycle
4+
5+
the directive is : lifeCycle
6+
7+
you can put in the <ion-view> element:
8+
9+
example
10+
<ion-view life-cycle view-title="Account" >
11+
12+
im using the view-title="xxxx" to identify the view;
13+
14+
when you navigate from and to that view you can see the console;
15+
16+
also you can perform some methods
17+
18+
you can see when you ente on account tabs, i add a number on top of the list even if the view is cached
19+
20+
thats is really usefull

www/js/controllers.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,37 +82,37 @@ angular.module('starter.controllers', [])
8282
.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
8383
var _str = 'ChatDetailCtrl'
8484
$scope.chat = Chats.get($stateParams.chatId);
85-
/* $scope.$on('$ionicView.loaded', function() {
86-
console.log(_str + ' loaded', $scope, $scope.$id)
87-
});
88-
$scope.$on('$ionicView.beforeEnter', function() {
89-
// Anything you can think of
90-
console.log(_str + ' BeforeEnter', $scope, $scope.$id)
91-
});
92-
$scope.$on('$ionicView.enter', function() {
93-
console.log(_str + ' enter', $scope, $scope.$id)
94-
// Anything you can think of
95-
});
96-
$scope.$on('$ionicView.afterEnter', function() {
97-
console.log(_str + ' afterEnter', $scope, $scope.$id)
98-
});
85+
/* $scope.$on('$ionicView.loaded', function() {
86+
console.log(_str + ' loaded', $scope, $scope.$id)
87+
});
88+
$scope.$on('$ionicView.beforeEnter', function() {
89+
// Anything you can think of
90+
console.log(_str + ' BeforeEnter', $scope, $scope.$id)
91+
});
92+
$scope.$on('$ionicView.enter', function() {
93+
console.log(_str + ' enter', $scope, $scope.$id)
94+
// Anything you can think of
95+
});
96+
$scope.$on('$ionicView.afterEnter', function() {
97+
console.log(_str + ' afterEnter', $scope, $scope.$id)
98+
});
9999
100-
$scope.$on('$ionicView.beforeLeave', function() {
101-
console.log(_str + ' beforeLeave', $scope, $scope.$id)
102-
// Anything you can think of
103-
});
104-
$scope.$on('$ionicView.leave', function() {
105-
console.log(_str + ' Leave', $scope, $scope.$id)
106-
});
100+
$scope.$on('$ionicView.beforeLeave', function() {
101+
console.log(_str + ' beforeLeave', $scope, $scope.$id)
102+
// Anything you can think of
103+
});
104+
$scope.$on('$ionicView.leave', function() {
105+
console.log(_str + ' Leave', $scope, $scope.$id)
106+
});
107107
108-
$scope.$on('$ionicView.afterLeave', function() {
109-
console.log(_str + ' afterLeave', $scope, $scope.$id)
108+
$scope.$on('$ionicView.afterLeave', function() {
109+
console.log(_str + ' afterLeave', $scope, $scope.$id)
110110
111-
});
112-
$scope.$on('$ionicView.unloaded', function() {
113-
console.log(_str + 'unloaded', $scope, $scope.$id)
114-
// Anything you can think of
115-
});*/
111+
});
112+
$scope.$on('$ionicView.unloaded', function() {
113+
console.log(_str + 'unloaded', $scope, $scope.$id)
114+
// Anything you can think of
115+
});*/
116116
})
117117

118118
.controller('AccountCtrl', function() {
@@ -121,4 +121,34 @@ angular.module('starter.controllers', [])
121121
vm.settings = {
122122
enableFriends: true
123123
};
124+
var _41 = 41;
125+
vm.setName = setName;
126+
vm.add41 = add41;
127+
128+
129+
activate()
130+
131+
function setName() {
132+
vm.name = 'pepito';
133+
}
134+
135+
function activate() {
136+
var arrayn = [];
137+
for (var i = 0; i < 40; i++) {
138+
var obj = {
139+
name: i,
140+
}
141+
arrayn.push(obj)
142+
}
143+
144+
vm.numbers = arrayn;
145+
}
146+
147+
function add41() {
148+
var obj = {
149+
name: _41,
150+
}
151+
_41++;
152+
vm.numbers.unshift(obj)
153+
}
124154
});

www/js/services.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ angular.module('starter.services', [])
5353
angular.module('starter.directives', [])
5454
.directive('lifeCycle', lifeCycle);
5555

56-
lifeCycle.$inject = ['$stateParams'];
56+
lifeCycle.$inject = ['$stateParams', '$timeout'];
5757

5858
/* @ngInject */
59-
function lifeCycle($stateParams) {
59+
function lifeCycle($stateParams, $timeout) {
6060
// Usage:
6161
//
6262
// Creates:
@@ -70,6 +70,7 @@ function lifeCycle($stateParams) {
7070
return directive;
7171

7272
function link(scope, element, attrs) {
73+
var idx = 0;
7374
console.time('loaded')
7475
console.time('beforeEnter')
7576
console.time('enter')
@@ -88,8 +89,12 @@ function lifeCycle($stateParams) {
8889
// Anything you can think of
8990
console.log(_nameView + ' BeforeEnter', scope, scope.$id)
9091
console.timeEnd('beforeEnter')
92+
console.log(idx);
93+
idx++;
9194
if (_nameView === 'Account') {
95+
$timeout(scope.vm.setName, 2000)
9296
scope.vm.settings.name = _nameView;
97+
scope.vm.add41();
9398
}
9499
});
95100
scope.$on('$ionicView.enter', function() {

www/templates/tab-account.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<ion-view life-cycle view-title="Account">
1+
<ion-view life-cycle view-title="Account" cache-view="true">
22
<ion-content>
33
<ion-list>
44
<ion-toggle ng-model="vm.settings.enableFriends">
@@ -8,5 +8,13 @@
88
<ion-item>
99
{{vm.settings.name}}
1010
</ion-item>
11+
<ion-item>
12+
{{vm.name}}
13+
</ion-item>
14+
<ion-list>
15+
<ion-item ng-repeat="number in vm.numbers">
16+
{{number.name}}
17+
</ion-item>
18+
</ion-list>
1119
</ion-content>
1220
</ion-view>

0 commit comments

Comments
 (0)