0

Hi I am developing web application in angularjs. I have implemented print functionality. I am binding values to view using scope variables. When i click first time on print my values are not binding. When i click second time all my values will bind. Below is the print functionality.

 $scope.Print = function () {
                if ($scope.period == null) {
                    toastr.error($filter('translate')('Please select Year.'));
                }
                else {
                $scope.vehiclepriceprint = $cookieStore.get("carpriceprint");
                $scope.downpaymentprint = $cookieStore.get("downpayment");
      }

First time also i will be having values in cookie but not binding to view. Whenever i click second time on print button everything will be working Below is the print button.

  <input type="button" value="{{'Print' | translate}}" class="blue-button"  ng-click="Print()" id="btnPrint">

Below is the html code of print.

   <span id="lblDownPayment">{{downpaymentprint}}</span>
   <span id="lblFinancialAmount">{{vehiclepriceprint}}</span>

May i know what is the issue i am facing here? Any help would be appreciated. Thank you.

8
  • please post the code where you're triggering your print function. i-e ng-click etc Commented Sep 28, 2017 at 5:24
  • Thank you. I have updated html. Commented Sep 28, 2017 at 5:28
  • Your angular js syntax is not valid. Commented Sep 28, 2017 at 5:29
  • May i know what would be the correct syntex? Commented Sep 28, 2017 at 5:30
  • If you are using this code in your web application then you are missing this -> }. Commented Sep 28, 2017 at 5:31

3 Answers 3

1

Try using $scope.$apply(); after updating the model values inside the controller. Now the view will get updated after that. This is a sample.

$scope.Print = function () {
      if ($scope.period == null) {
           toastr.error($filter('translate')('Please select Year.'));
      }
      else {
           $scope.vehiclepriceprint = $cookieStore.get("carpriceprint");
           $scope.downpaymentprint = $cookieStore.get("downpayment");
           $scope.$apply(); // To update view
      }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. In controller or inside the print function i should apply?
You should apply it in inside the print function, to update the view. I have updated my answer.
0

Try using $apply. Although this is not a good approach.

$scope.Print = function () {
                    if ($scope.period == null) {
                        toastr.error($filter('translate')('Please select Year.'));
                    }
                    else {
                    $scope.vehiclepriceprint = $cookieStore.get("carpriceprint");
                    $scope.downpaymentprint = $cookieStore.get("downpayment");
                    $timeout(function(){//inject $timeout as dependency
                       $scope.$apply();
                    })
          }

Edit 1: Try this approach. here I have used Object (key:value) ways to update the print data.

$scope.printData = {};

$scope.Print = function () {
                    if ($scope.period == null) {
                        toastr.error($filter('translate')('Please select Year.'));
                    }
                    else {
                    $scope.printData.vehiclepriceprint = $cookieStore.get("carpriceprint");
                    $scope.printData.downpaymentprint = $cookieStore.get("downpayment");

          }

HTML:

<span id="lblDownPayment">{{printData.downpaymentprint}}</span>
   <span id="lblFinancialAmount">{{printData.vehiclepriceprint}}</span>

6 Comments

I have added but no luck
Thanks for your answer. Still same problem. Whenever i click second time i will get required values
@NiranjanGodbole can you if are you getting value form $sookiestore on first click.
Hi Ved, I am getting on first click. I just displayed in console. also i debugged but binding to view is not happening
have you tried the my edited answer approach exactly same?
|
0

Analysis

Actually, ngClick calls $scope.$apply() under-the-hood (in the safest way possible). So you should not call it explicitly in your Controller.

Problem is that the variables you are trying to display/update are attached directly to the $scope, and this approach usually causes this issue.

Solution

$scope.vm = {};

$scope.Print = function() {
    $scope.vm.vehiclepriceprint = $cookieStore.get("carpriceprint");
};
<span id="lblFinancialAmount">{{ vm.vehiclepriceprint }}</span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.