0

click i dosen't get the form data when button is outside form

code

<ion-view title="Indstillinger">

<ion-nav-buttons side="right">
    <button class="button" form="userForm" ng-click="update()">
        Gem
    </button>
</ion-nav-buttons>


<ion-content padding="true" class="has-header">
    <form id="userForm">

        <ion-list>


            <label class="item item-input" name="navn">
                <span class="input-label">Navn</span>
                <input type="text"  placeholder={{user.name}}  ng-model="name">
            </label>


        </ion-list>
    </form>
</ion-content>

i tried add form id to form and button, but i still dosen't get the form data on ngclick :/

update:

 $scope.update = function() {
        console.log(this.name);
    })
Stweet
  • 683
  • 3
  • 11
  • 26
  • show your javascript , and show enough of your HTML to include ng-app and ng-controller or whatever related ng attributes – Scott Selby May 24 '15 at 17:49

4 Answers4

2

ion-content creates a child scope. The input element is setting the "name" property on this scope, not the scope being used in the ion-nav-buttons section.

This is the classic "dot notation" issue in Angular.

If you are using an "outer" controller:

<div ng-controller="outer as vm">
    <ion-view title="Indstillinger">

    <ion-nav-buttons side="right">
        <button class="button" form="userForm" ng-click="update()">
            Gem
        </button>
    </ion-nav-buttons>


    <ion-content padding="true" class="has-header">
        <form id="userForm">

            <ion-list>


                <label class="item item-input" name="navn">
                    <span class="input-label">Navn</span>
                    <input type="text"  placeholder={{user.name}}  ng-model="vm.name">
                </label>


            </ion-list>
        </form>
    </ion-content>
</div>

.controller('outer', function($scope) {
    $scope.user = {
        name: "Joe"
    }
    $scope.update = function()
    {
        console.log($scope.vm.name);
    }
})
Joe Enzminger
  • 11,110
  • 3
  • 50
  • 75
1

You will want to use:

console.log($scope.name);

instead of:

console.log(this.name);
PhuLuong
  • 527
  • 4
  • 11
0

since you didn't post the rest of HTML or the rest of controller code , this is the only answer I can possibly provide

 $scope.update = function() {
        console.log($scope.name);
    })

I know you said that it doesn't work , but that code is in fact valid and does work. Something else that you refused to post is breaking

Did you check console for errors? I'm sure there are some, did you confirm that angular is properly loaded, did you properly set add ng-app and ng-controller ?

Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • $scope.update is the only thing in controller besides scope.user... and this is all html for this form.. so all code need'ed is here – Stweet May 24 '15 at 18:10
0

1) Try moving your <button ng-click="update"> outside of your <ion-nav-buttons> container and see if it works.

2) If it works, read this: Transcluding ng-click in a directive

Community
  • 1
  • 1
Alextoul
  • 839
  • 3
  • 9
  • 19