2

I'm using AngularJS and jQuery for a project. When i typing input, ng-change is working.. But, when i using jquery input.val('blabla') ng-change is not working.. How can i report this change angularjs side? This is my code.. Apply or watch or other one?

 // Html
    <input type="text" name="city" class="city-input req-string" rel="cityCtr" value="" ng-model="city" ng-change="findWeather(city)">

    // jQuery code
    $('.city-input').val('İstanbul');

     // All AngularJS code
    var app = angular.module('weatherApp', []);

app.controller('weatherCtrl', ['$scope', 'weatherService', function($scope, weatherService) {

    function fetchWeather(city) {
        weatherService.getWeather(city).then(function(data){
            $scope.items = data;
        });
    }

    $scope.findWeather = function(city) {
        $scope.items = '';
        fetchWeather(city);
        alert(city);
    };

}]);

app.factory('weatherService', ['$http', '$q', function ($http, $q){
    function getWeather (city) {
        var deferred = $q.defer();
        var query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'+city+'")',
            url   = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&callback=";
        $http.get(url)
            .success(function(data){
                deferred.resolve(data.query.results.channel.item.forecast);
                console.log(data)
            })
            .error(function(err){
                console.log('Error retrieving markets');
                deferred.reject(err);
            });
        return deferred.promise;
    }

    return {
        getWeather: getWeather
    };
}]);
10
  • did you check for any error message in browser console? Commented Feb 7, 2015 at 10:26
  • You probably just need to force a digest cycle with $scope.$apply or $timeout. Commented Feb 7, 2015 at 10:34
  • Where should i use $scope.$apply or timeout? in findWeather function? Commented Feb 7, 2015 at 10:39
  • possible duplicate of AngularJS ng-model binding not updating with dynamic values Commented Feb 7, 2015 at 10:45
  • another Possible duplicate: stackoverflow.com/questions/22612650/… Commented Feb 7, 2015 at 10:46

1 Answer 1

2

Just use:

$('.city-input').val('İstanbul').trigger('input');

Instead of:

$('.city-input').val('İstanbul');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.