Get value of $model property on click


#1

Hi,

I’m testing ionicframework and I have some doubts. I’ve never used angular, but i’m trying to do my best :wink:

My code is from the tabs example app, I’ve updated tab-dash.html to this:

<ion-view title="Dashboard">
  <ion-content class="padding">
    <h1>Dash</h1>
    <p>{{prueba}}</p>
    <input type="text" ng-model="prueba">
    <a ng-click="click()">prueba</a>
  </ion-content>
</ion-view>

And in my controllers.js

.controller('DashCtrl', function($scope) {
    $scope.prueba = 'un valor';

    $scope.click = function(){
        console.log($scope.prueba);
    }
})

I just want to log what I have changed in the input. When I try to access to $scope.prueba, it alwais have the same value.

I know this is a super newbie question, but I really need help with this.

Thank you very much in advance.


#2

http://plnkr.co/edit/K1P9At?p=preview Should solve it for you.

Was fun chatting on irc. Ask more questions if you have an issue.

Best of luck!


#3

@chux, http://stackoverflow.com/questions/12618342/ng-model-does-not-update-controller-value will probably solve all your problems, especially the second problem.
The question in SO is exactly as you posted here


#4

Hi,

Thank you very much to both of you!!! I’ve got it working now.


I still don’t get why if I change the value, only if I use object.something is working.

The stack overflow link was clarifying, if I want two way data binding I should use $scope.object.var.

@ioudas it was fun for me too :wink: thanks for your time. You guys are awesome.


#5

Anytime chux. I am a little confused by this comment as the main plunk i showed you used a localized scope variable without any dot notation and it works.


#6

@ioudas I’m still too confused :wink: I’ve purchased “mastering web application development with angular js” from packtpub and, in page 19 (regarding to two-way data binding):

“Avoid direct bindings to scope’s properties. Two-way data binding to object’s properties (exposed on a scope) is a preferred approach.
As a rule of thumb, you should have a dot in an expression provided to the ng-model directive (for example, ng-model=“thing.name”).”

As I’m very new to angular, I will do that that way until I’m more confident. Thanks!


#7

That would be a good recommendation. For sure. Its not the end of the world either way. IMHO.