I am having serious difficulties updating a view in Ionic/Angular when using ng-click
.
I came across a similar issue earlier on but solved it following this guidance, which explains that if your button is inside a < label >
ng-click will not work.
What am I trying to achieve?
I am developing a mobile application and want to allow a user edit their details if necessary. In an effort to start off most simply I have began by trying to implement the functionality whereby a user can edit their name. In the example below I am trying to change the name of the user when a button is clicked.
The strange thing is when I console.log(Photos.name)
I get the correct value, i.e.(scope.user.name = "Jim"
) however the view does not reflect this change.
The code I have is as follows:
Controller
.controller('ProfileCtrl', function($scope, Photos) {
$scope.user = {
"name": Photos.name
}
$scope.changeName = function() {
Photos.changeName();
console.log(Photos.name)
};
})
Factory:
.factory('Photos', function(){
var o = {
name : 'James'
}
o.changeName = function(){
o.name = 'Jim'
}
return o;
});
HTML Settings Template
This is where the button is that updates the user's name on the profile.html page(see below)
<ion-view view-title="Settings">
<ion-content>
<ion-list>
<div class="item item-input">
<span class="input-label">Change Profile Picture</span>
<button type="button" ng-click="changeName()">Add</button>
</div>
</ion-list>
</ion-content>
</ion-view>
Profile Template
<ion-view view-title="My Profile">
<ion-content class="padding">
<div class="item item-body">
<p class="profile-details">
{{user.name}}
</p>
</div>
</ion-content>
</ion-view>
I have read through many SO answers and those on the ionic webpage, with no luck. From what I have read I think that it may be something to do with setting the value in a child scope is the thing that is breaking it but I cannot be sure.
Can anyone help?