0

I'm working on a project in Rails that also uses AngularJS. First of all, I have a variable called @resource that contains an end time

<input style="display:none" ng-model="remainingTime" ng-init="remainingTime = <%= (resource.available_until.to_f - DateTime.now.to_f).to_i %>">

This obtains the remaining time for an X task and is displayed as follows (without display none).

A input with a remaining time

That value obtained I must access it from the controller.

In the controller I have the following:

 function UserEvaluationsNewCtrl ($log, $scope) {
    console.log($scope.remainingTime)
}

When I want to access from the controller, the variable show "undefined" :/

Any help is useful. Thank you!!!

Edited:

console.log($scope): enter image description here

remainingTime=-39698

-The negative number is correct-

Edited 2: Complete controller:

;(function () {
  'use strict';

  // Define controller
  function UserEvaluationsNewCtrl ($log, $scope) {
    $log.debug('UserEvaluationsNewCtrl: Hi')
    var vm = this

    vm.data = angular.extend({}, rails_data || {})
    vm.evaluationData = vm.data.resource
    vm.evaluationDataStringified = ''

    $scope.$watch(function(){
      return vm.evaluationData
    }, function(newValue, oldValue){
      vm.evaluationDataStringified = angular.toJson(newValue)
      $log.debug('ResourcesNewCtrl: $watch vm.evaluationDataStringified: to JSON output:', vm.evaluationDataStringified)
    }, true)

    console.log($scope)
  }

  // Inject dependencies
  UserEvaluationsNewCtrl.$inject = ['$log', '$scope']

  // Register controller
  angular
    .module('app.controllers.user_evaluations.new', [])
    .controller('UserEvaluationsNewCtrl', UserEvaluationsNewCtrl)
})();
8
  • what do you get when you log $scope ? Commented Feb 7, 2018 at 22:42
  • can you change your console.log(test) line to console.log($scope) and update the question with the output you get Commented Feb 7, 2018 at 22:56
  • @Subash ok! Edited! Commented Feb 7, 2018 at 22:59
  • @Subash PD: The negative number is correct Commented Feb 7, 2018 at 23:00
  • try console.log($scope.formCtrl.remainingTime) Commented Feb 7, 2018 at 23:02

2 Answers 2

1

Reason: The controller is being created before the remainingTime variable is added to the $scope object. Don't trust your console.log with object.

Solution:

  1. Use $watch

    $scope.$watch('remainingTime', function() { alert($scope.remainingTime); });

  2. or Use $timeout

    $timeout(function () { alert($scope.remainingTime); });

Sign up to request clarification or add additional context in comments.

2 Comments

It's true, the alert shows the correct time remaining but how can I use the variable in a function? Really $scope.remainingTime contains the time? When passed as an argument of the countdown function the variable does not exist (undefined)
I made it! I called the function instead of the alert! Thank you very much!
0

I think what is happening is:

  1. Your controller initializes the empty value.
  2. You have connected your div to the variable, which it can’t have.
  3. You have assigned a value to the <input> field, which is not connected to the $scope.remainingTime variable.

Try using the change below which should do the following:

  1. At html page init (which completes after the controller init) use <input ng-init=... to set the value of remainingTime.
  2. Attach the <input> field to the remainingTime scope variable.

    <input style="display:none" ng-init="remainingTime = <%= (resource.available_until.to_f - DateTime.now.to_f).to_i %>)" />
    <div >
       <input ng-model="remainingTime"> 
    </div>
    

You may be able to (should be able to?) use both ng-init and ng-model together on the real <input> field, but I've had uncertain results doing that.

Cheers, Gary.

1 Comment

Thanks for answering!. I edited the question with your clarifications. The problem is almost the same :( any ideas?

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.