Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass input value to controller on enter or button click

I'm trying to get started learning AngularJS for an Ionic app I'm working on, and I'm having a little trouble understanding AngularJS having had most previous experience on jQuery which focuses on DOM manipulation rather than frameworking.

If I have the following markup:

<label class="item-input-wrapper">
  <i class="icon ion-ios7-chatbubble placeholder-icon"></i>
  <input type="text" placeholder="Send a message...">
</label>
<button class="button button-clear button-positive">
  Send
</button>

How can I pass the value of the input on to the controller when enter or send is clicked? I'm working on a chat app, so I believe that a model approach is needed so that the message thread can be automatically updated but other than that I have no idea.

Could someone help me out or at least point me in the right direction?

like image 328
Benedict Lewis Avatar asked Jul 26 '14 19:07

Benedict Lewis


1 Answers

There are several ways to pass value to your controller. Here is the simplest example. As Justin said, you should look into angular basics.

HTML:

<div ng-controller="MyCtrl">
     <input type="text" ng-model="foo" placeholder="Enter something" />
     <input type="button" ng-click="doSomething()" value="Send" ng-disabled="foo == null" />
</div>

JS:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.foo = null;
    $scope.doSomething = function () {
        alert("Hello, " + $scope.foo);
    }
}

Here is the working fiddle

And I would recommend you to go through this post.

like image 92
Umayr Avatar answered Oct 13 '22 06:10

Umayr