[SOLVED] Get "input value" in to controller

Hi, I need to get into the controller the value of an input field.

<script id="tpl/carica.html" type="text/ng-template">
<ion-view view-title="Carica">
	<ion-content class="padding">
		<div class="list list-inset">
			<label class="item item-input">
				<input type="number" id="codice_prodotto" name="codice_prodotto" ng-model="codice_prodotto">
		<button nav-direction="back" class="button button-block button-large button-balanced" ng-click="carica()">CARICA</button>

app.controller('CaricaCtrl', function ($scope, sendAndLoad) {
    // $scope.scanBarcode = function() {
    $scope.carica = function() {
	console.log('codice prodotto: '+ $scope.codice_prodotto);



Just like you used it.

Is your controller bound to tpl/carica.html?

yes my controller is:

.state('carica', {
	url: "/carica",
	templateUrl: "tpl/carica.html",
	controller: 'CaricaCtrl'

I seem to have used $scope.codice_prodotto not?

I’m just guessing, I think your problem is this: sendAndLoad

Do you have this service and have you looked at your console logs?

If that object does not exist controller execution will fail.

Yes i’have this service:

app.service('sendAndLoad', function() {
	this.posM = '';
	this.posS = '';
	this.posP = '';

What about console log?

Are you seeing this output:

console.log(‘carica’); ?

And what about this one:

console.log($scope.codice_prodotto); ?

Have you received any error for this last line?

The output is:

2015-07-28 20:11:35.221 test[4500:2932243] carica
2015-07-28 20:11:35.222 test[4500:2932243] carica2

With this code:


Pass the model object as parameter in the function try using below ng-click="carica(codice_prodotto)"

and in the controller try using below pass the parameter i think it is not getting the scope of the input field
$scope.carica = function(codice_prodotto) {
console.log('codice prodotto: '+ $scope.codice_prodotto);

I got another idea, I so this once but completely forgot about it. Instead of this;

<input type="number" id="codice_prodotto" name="codice_prodotto" ng-model="codice_prodotto">

use this:

<input type="number" id="codice_prodotto" name="codice_prodotto" ng-model="codice_prodotto.value">

And in your controller:



You need to use dot notation with Ionic and input elements placed inside a ion-content container:

I had already tried it, but it was printing even ‘console2’

I tried but I will return undefined :frowning:

Thank you both, so I solved.

<input type="tel" id="codice_prodotto" name="codice_prodotto" ng-model="settings.codice_prodotto">

<button nav-direction="back" class="button button-block button-large button-balanced" ng-click="carica(settings)">CARICA</button>

$scope.carica = function(settings) {
	console.log('codice prodotto: '+ settings.codice_prodotto);

So dot notation was a fix :smile:

1 Like

Yes, with a different use :slight_smile:

1 Like
.controller('HomeCtrl', function($scope) {
  $scope.AddItem = function(data){
      item : "Repair TV",
      status : false,
      item : "Do Nothing",
      status : true,

help me with this

<div class="bar bar-footer item-input-inset">
        <label for="" class="item-input-wrapper">
          <input id="homeNewItem" type="text" placeholder="New Home ToDo" value="hello">
        <button class="button button-small" ng-click="AddItem(document.getElementById('homeNewItem').value)">Add</button>