javascript - Why printing multiple times on calling a function - AngularJS -
i calling function controller scope, in console values printed 3 times. why happening?
source
<!doctype html> <html ng-app="mymodule" > <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> </head> <body ng-init="pricelist='promo03,promo04'"> <div ng-controller="pricingcontroller" > {{splitarray()}} </div> <script> var mymodule = angular.module('mymodule',[]); mymodule.controller('pricingcontroller',['$scope', function($scope){ $scope.pricestring = $scope.pricelist; $scope.array = []; $scope.splitarray= function(){ console.log($scope.pricestring); $scope.array = $scope.pricestring.split(","); console.log($scope.array[0]); console.log($scope.array[1]); }; }]); </script> </body> </html>
console output
promo03,promo04 promo03 promo04 promo03,promo04 promo03 promo04 promo03,promo04 promo03 promo04
expected output
promo03,promo04 promo03 promo04
this called every digest loop of angular. if keep program running, you'll have more logs.
to prevent it, call function controller, not binded value html.
for instance :
$scope.splitarray= function(){ console.log($scope.pricestring); $scope.array = $scope.pricestring.split(","); console.log($scope.array[0]); console.log($scope.array[1]); }; $scope.splitarray();
Comments
Post a Comment