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">
</label>
</div>
<button nav-direction="back" class="button button-block button-large button-balanced" ng-click="carica()">CARICA</button>
</ion-content>
</ion-view>
</script>
app.controller('CaricaCtrl', function ($scope, sendAndLoad) {
console.log('carica');
console.log($scope.codice_prodotto);
// $scope.scanBarcode = function() {
$scope.carica = function() {
console.log('codice prodotto: '+ $scope.codice_prodotto);
...
coachdesign:
codice_prodotto
It’s:
$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?
coachdesign:
sendAndLoad
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:
console.log('carica');
console.log($scope.codice_prodotto);
console.log('carica2');
Pass the model object as parameter in the function try using below ng-click="carica(codice_prodotto)"
CARICA
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:
console.log($scope.codice_prodotto.value);
###EDIT
You need to use dot notation with Ionic and input elements placed inside a ion-content container:
Hi,
scope variables are undefined inside controller function if input field with ng-model is inside ion-content.
If I place the same input field outside ion-content, everything works as expected.
I think I maybe missing something because if I recall properly several versions back that worked as it should.
Please help.
Thanks
I had already tried it, but it was printing even ‘console2’
I tried but I will return undefined
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(settings);
console.log('codice prodotto: '+ settings.codice_prodotto);
2 Likes
So dot notation was a fix
1 Like
Yes, with a different use
1 Like
AnniDa
July 10, 2017, 4:48am
16
.controller('HomeCtrl', function($scope) {
$scope.AddItem = function(data){
console.info(data)
};
$scope.items=[
{
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">
</label>
<button class="button button-small" ng-click="AddItem(document.getElementById('homeNewItem').value)">Add</button>
</div>