范围过滤器仅适用于第一个控制器

时间:2015-11-29 20:49:45

标签: javascript html angularjs

我有范围过滤器(下面的代码),它只适用于我的第一个控制器。 我在app.js和html中以相同的方式在其他控制器中添加了这个过滤器而没有工作,我不知道为什么?

在控制台中我没有错误。

范围过滤器代码:

appPokayoke.filter('rangeFilter', function() {
    return function(input, min, max) {
        min = parseInt(min); //Make string input int
        max = parseInt(max); //Make string input int

        for (var i=min; i<max; i++)
            input.push(i);
            return input;
    };
});

我的app.js文件:

(function(){


    var appPokayoke = angular.module('appPokayoke', ['ngRoute', 'pyService']);

    appPokayoke.config(['$routeProvider', function($routeProvider) {

        code ...

    }]);

    appPokayoke.controller('DrawingsListCtrl', ['$scope', 'drawings', 'pantons', function ($scope, drawings, pantons) {

        code ...

    }]);

    appPokayoke.controller('DrawingDetailsCtrl', ['$scope','drawings', 'articles', '$routeParams', 'articlesOfDrawings', 'pantonClass', function ($scope, drawings, articles, $routeParams, articlesOfDrawings, pantonClass, rangeFilter ) {

        code ...

    }]);

    appPokayoke.controller('CreateArticleCtrl', ['$scope', '$http','pantons','$routeParams', 'articles', '$location', function ($scope, $http, pantons, $routeParams, articles, $location, rangeFilter) {

        code ...

    }]);

    appPokayoke.controller('CreateArticleColorsCtr', ['$scope', '$http','$routeParams', 'articles', '$location', 'articlesOfDrawings', function ($scope, $http, $routeParams, articles, $location, articlesOfDrawings, rangeFilter ) {

        code ...

    }]);

    appPokayoke.filter('rangeFilter', function() {
        return function(input, min, max) {
            min = parseInt(min); //Make string input int
            max = parseInt(max); //Make string input int

            for (var i=min; i<max; i++)
                input.push(i);
                return input;
        };
    });

})();

in html:

<table class="table table-bordered table-hover table-striped">
    <thead>
        <tr>
            <th width="7%">Lp.</th>
            <th  width="10%">Article <i class="fa fa-chevron-down"></i> </th>
            <th width="10%">Colors in article</th>
            <th ng-repeat="m in [] | rangeFilter:0:pyNumber track by $index">{{$index+1}}</th>
        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="article in articlesOfDrawings | filter : search track by $index">
            <td>{{$index+1}}</td>
            <td ng-repeat="column in article" style="background-color: rgb({{column[1]}});"  ><p>{{ column[0] }}</p></td> 
        </tr>
    </tbody>
</table>

修改 过滤器不工作的控制器代码:         appPokayoke.controller('CreateArticleColorsCtr',['$ scope','$ http','$ routeParams','articles','$ location','articlesOfDrawings',function($ scope,$ http,$ routeParams,articles, $ location,articlesOfDrawings,rangeFilter){

        var drawingId = $routeParams.drawingId;
        var pyNumber = $routeParams.pyNumber;
        console.log(pyNumber);

        $scope.rangeFilter = function(max) {
          return max = pyNumber;
        };

       $scope.articleColors = {};
       $scope.articleColorsData = {};

       articles.getNewArticle(
            $routeParams.articleName, 
            $routeParams.drawingId,
            function (data) {
                if(data !== '') {
                    $scope.articleColors = data;
                }
                else {
                    console.log('not ok articleColors');  
                }
            }
        );

        articlesOfDrawings.getArticlesOfDrawings(
            $routeParams.pyNumber, 
            $routeParams.drawingId,
            function (data) {
                if(data !== '') {
                    $scope.articlesOfDrawings = data;
                }
                else {
                    console.log('not ok articlesOfDrawings');  
                }
            }
        );


        $scope.saveArticleColors = function (articleColorsData, success) {

            success = success || function() {};

            $http.post('api/admin/articles/save_colors/', 
                articleColorsData ).success(function (data){
                console.log(data);
                    success(data.articleColorsData);
                    $scope.articleColorsData = articleColorsData;
                    console.log($scope.articleColorsData);
                    //$location.path('/drawings/'+ drawingId + '/' + pyNumber);

            }).error(function (){
                console.log('Problem ArticleColors');
            });

        }

    }]);

1 个答案:

答案 0 :(得分:1)

将pyNumber放在您的范围内,以便您可以从过滤器中调用它:

$scope.pyNumber = $routeParams.pyNumber;
        console.log($scope.pyNumber);

      // you don't need this function 
      //  $scope.rangeFilter = function(max) {
      //    return max = pyNumber;
      //  };