Reputation: 1621
I need to update phone number to the controller when it is taken from a dial pad. When dial pad exceed 9 digits console log showing the number but it is not updated in Controller.Inside the controller function I added alert. So alert showing undefined variable.
var app = angular.module('myApp', []);
app.controller('PosController', function ($scope, $http) {
$scope.phonenumber="";
var url = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;
$scope.updatePhone = function (id) {
$scope.phonenumber=id;
alert('Here the number should be, but is not: '+ id + "---:" + $scope.phonenumber);
$scope.phonenumber="";
};
});
<div ng-app="myApp" ng-controller="PosController" class="panel" >
<div class="input-group col-xs-4">
<div class="input-group-btn">
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#myModal">Telefono</button>
</div><!-- /btn-group -->
<div ng-app="myApp" ng-controller="PosController">
<input id="phonenumber" class="form-control" ng-model="phonenumber" />
<!--<input type="text" id="phonenumber" ng-model="myModel" ng-keyup="(myModel.length >= 3) && myFunction()" class="form-control" data-inputmask='"mask": "(999) 999-9999"' data-mask>-->
</div>
<div class="input-group-btn" >
<button type="button" class="btn btn-success">Cliente</button>
</div><!-- /btn-group -->
<div ng-app="myApp" ng-controller="PosController">
<input type="text" id="cliente" onclick="valor();" class="form-control" ng-focus="updatePhone($('#phonenumber').val());">
</div>
</div>
</div>
function updatePhoneNumber(btnpressed){
$("#phonenumber").val($("#phonenumber").val() + btnpressed);
if ($("#phonenumber").val().length >9){
console.log('validando '+ $("#phonenumber").val());
$('#cliente').focus();
$('#myModal').modal('hide');
}
}
Upvotes: 1
Views: 57
Reputation: 1149
There is alot "wrong" in your code, but angular takes time when you come from a JQuery world. A rule of thumb: "Don't use JQuery with Angular".
I've made a simple plunker to illustrate, how you can achieve your goal in a simple way:
https://plnkr.co/edit/K6G95OFsaYt3AAJVujZf?p=preview
Study it a bit.
The most important points:
1. Dont use ng-app
and ng-contoller
all the time.
2. Use ng-click
to handle click events
3. JQuery is to no use here.
The controller:
app.controller('MainCtrl', function($scope) {
$scope.phonenumberFromDial = "";
$scope.phonenumber = "";
var url = window.location.protocol + "://" + window.location.host + "/" + window.location.pathname;
$scope.updatePhoneNumber = function() {
if($scope.phonenumberFromDial.length > 9) {
console.log("Log phonenumber: " + $scope.phonenumberFromDial);
} else {
$scope.phonenumber = $scope.phonenumberFromDial;
}
};
});
View
<div>
Phonenumber from dial:
<input type="text" ng-model="phonenumberFromDial"/>
<br>
<button ng-click="updatePhoneNumber()">Save Phonenumber</button>
<div>
Saved phonenumber in controller: {{phonenumber}}
</div>
</div>
This is a simple plunker just for understanding the basics. I hope I've understood your problem correctly.
Upvotes: 1
Reputation: 21
You should not combine JQuery and AngularJs. What you are trying to achieve can be done with a simple routine in the controller.
Try use ng-click in the html file to call your routine for phone
AngularJs and JQuery should only be used in custom directives or components
Upvotes: 1