0

I have this input field in my form

<input type="number" style="display:none" ng-model="frequency" id="frequencyDisplay">
            </div>

The value of this input field is changed when I change a slider, given by this jquery

    $(".dial").knob({
        'min':1,
        'max':180,
        change: function(value){
            $("#frequencyDisplay").val(Math.round(value));
            console.log(value);
        }
    });

However, I can't access the ng-model frequency as it seems to be blank.

In my controller,

$scope.frequency is a null value. It's got nothing attached to it. I've most definitely checked using the console that #frequencyDisplay has a value. Just ng-model is unable to access it. Any ideas how to?

user3340037
  • 731
  • 2
  • 8
  • 23
  • 1
    If you haven't yet encountered in pretty much any article about intro to Angular, I'll say it here: mixing Angular with jQuery is a bad idea; you really need to know what you are doing, and even so, restrict it to directives. – New Dev Dec 21 '14 at 23:20
  • Didn't feel comfortable enough with directives, but I'll probably practice more with them, thanks! – user3340037 Dec 21 '14 at 23:21
  • Read this [SO answer](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) - it will help you to think in Angular way – New Dev Dec 21 '14 at 23:23

2 Answers2

0

The scope probably needs to be updated - using $scope.apply() will make angular look for any changes inside your controller/directive.

https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest

Jeff Ling
  • 1,032
  • 9
  • 12
0

Something like this should do the trick (:

angular.module('myApp', [])
  .controller('myCtrl', function($scope, $timeout) {
    $scope.frequency = 0;    
    $scope.setFrequency = function(value) {
      $timeout(function(){ 
        $scope.frequency = value;
      });
    }
  });


$(function() {
  $("#target").on("change paste keyup", function() {
    var value = Math.round($(this).val());
    var e = document.getElementById('frequencyDisplay'); 
    var $timeout = angular.element(e).injector(['ng']).get('$timeout');    
    var scope = angular.element(e).scope();
    scope.setFrequency(value);    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<body ng-app="myApp" ng-controller="myCtrl">
  <input type="number" id="target" />
  <input type="number" ng-model="frequency" id="frequencyDisplay" />
</body>
Rogerio Soares
  • 1,613
  • 16
  • 14