Data Bind to Text Field Value from Controller

43 views
Skip to first unread message

ranjeet salunkhe

unread,
Feb 25, 2015, 6:27:39 AM2/25/15
to ang...@googlegroups.com
Hi All,

I have following sceniro in my current application

- i wrote Barcode scanner code in Factory Method
  app.factory("getBarcode",function($q)
{
var dataService={};
dataService.codevalue;
dataService.scanCodeService=function(){
var deferred=$q.defer();
var scanner = cordova.require("cordova/plugin/BarcodeScanner");
   scanner.scan(
       function(result) {
     //  alert("Scanned Code: " + result.text  + ". Format: " + result.format  + ". Cancelled: " + result.cancelled);
        deferred.resolve(result.text);
dataService.codevalue=result.text;
   }, function(error) {
       //alert("Scan failed: " + error);
    deferred.reject(error.text);

   });
return deferred.promise;
}
return dataService;
}
);

   This will return me the Scanned Text.

- In Controller i wrote following thing
  app.controller('addMeterController',['$scope', '$ionicActionSheet', '$cordovaCamera','getBarcode',
        function($scope, $ionicActionSheet, $cordovaCamera,getBarcode)
        {

                      $scope.barcode=function(){
getBarcode.scanCodeService()
.then(function(data)
{
console.log('Data: -------------------------- >'+data);
    $scope.meterno=data;
 
console.log('meterno : -------------------------- >'+$scope.meterno);
},function(data)
{
alert('error');
}
)
};
 
                  }]); 

- My Html Contain Following part
 <div class="row">
<div class="col-75"><input type="number" placeholder="Enter an existing Meter Number" ng-model="meterno" ng-bind={{meterno}} value={{meterno}} name="txtmeterno" ngMinlength="0"
ngMaxlength="10" required ng-pattern="/^(\d)+$/"></div>
<div class="col-25"><button class="button button-dark" ng-click="barcode()">Scan</button></div>
</div>
  I am refering meterno from controller. Barcode scaning functionality is working fine on clicking of scan button . It print the console log. I want to bind whatever value come from Barcode function to Text Field. First it came to text field but after that it will not come. How can i bind my scanned result text to text field.

Thanks in advance

Sander Elias

unread,
Feb 25, 2015, 6:48:54 AM2/25/15
to ang...@googlegroups.com
Hi,

You scan events is coming from 'outside' angular. To make sure the view gets the updated data you should add an `$scope.$apply(...)` to your function.
I'm not sure why resolving a promise does not take care for that in this case tough?

Regards
Sander

ranjeet salunkhe

unread,
Feb 25, 2015, 7:16:04 AM2/25/15
to ang...@googlegroups.com
Hi Sander,

Thanks for prompt reply. I added `$scope.$apply block in barcode controller. it was giving me $digest already in progress.

$scope.barcode=function(){
getBarcode.scanCodeService()
.then(function(data)
{
console.log('Data: -------------------------- >'+data);
$scope.$apply(function () {
$scope.meterno=data;
});
    // $scope.meterno=data;
 
console.log('meterno : -------------------------- >'+$scope.meterno);
},function(data)
{
alert('error');
}
)
};


I added 


Regards,
Ranjeet

Sander Elias

unread,
Feb 25, 2015, 10:05:47 AM2/25/15
to ang...@googlegroups.com
ok, can you remove the `$scope.$apply` and then put in this line just below the `$scope.meterno=data;`
THIS IS FOR TESTING ONLY!! DON"T LEAVE IT IN LIKE THIS!
setTimout(function () {$scope.$apply()},0);

Does that help?

Regards
Sander

ranjeet salunkhe

unread,
Feb 26, 2015, 12:55:24 AM2/26/15
to ang...@googlegroups.com
Hi Sander,

I am getting following error message in android log cat

ReferenceError: setTimout is not defined

Br,
Ranjeet

Sander Elias

unread,
Feb 26, 2015, 5:57:46 AM2/26/15
to ang...@googlegroups.com
try setTimeout...

ranjeet salunkhe

unread,
Feb 26, 2015, 8:36:15 AM2/26/15
to ang...@googlegroups.com
hi Sander,

How much timeout i would set for this one. Could you please ping me if you have any timeout code available.


On Thursday, February 26, 2015 at 4:27:46 PM UTC+5:30, Sander Elias wrote:
try setTimeout...

Sander Elias

unread,
Feb 26, 2015, 10:35:53 AM2/26/15
to ang...@googlegroups.com
10ms
Reply all
Reply to author
Forward
0 new messages