使用ngChange,Angular后,向$ resource添加新参数

时间:2013-11-12 14:56:49

标签: javascript angularjs angular-resource

在选择要为API创建请求的类型后,我无法尝试附加新的url参数。

我的ng-changegenreChange。选中后,它应自动在&with_genre=fiction网址中附加新的网址参数$scope.movies,然后再提交submitButton

<form ng-submit="submitButton()" name="cForm" class="form-horizontal">
    <h2>Discover the gems</h2>       
        <div class="form-group">
            <select class="form-control" ng-model="genreSelect" ng-change="genreChange()">
                <option ng-repeat="genre in genreList.genres" value="{{genre.id}}">{{genre.name}}</option>
             </select>    
        </div>

        <input type="submit" value="hello" />           
</form>

-

$scope.genreChange = function() {
    $scope.genreVar =  $scope.genreSelect;
    $scope.movies.get({with_genres: $scope.genreVar});
}

$scope.movies = $resource('http://api.themoviedb.org/3/discover/movie:action', {
    api_key: apikey,
    callback: 'JSON_CALLBACK'
}, {
    get: {
        method: 'JSONP'
    }
});

$scope.submitButton = function() {
    $scope.films = $scope.movies.get();
}

我正在使用此方法,以防用户将其留空。

由于

1 个答案:

答案 0 :(得分:0)

这就是Resource.bind所做的。

var MovieResource = $resource('http://api.themoviedb.org/3/discover/movie:action', {
    api_key: apikey,
    callback: 'JSON_CALLBACK'
}, {
    get: {
        method: 'JSONP'
    }
});

$scope.movies = MovieResource;

$scope.genreChange = function() {
    $scope.genreVar =  $scope.genreSelect;
    $scope.movies = MovieResource.bind({with_genres: $scope.genreVar})
}

$scope.submitButton = function() {
    $scope.films = $scope.movies.get();
}