在$ scope内调用一个函数。$ watch

时间:2014-11-23 01:53:40

标签: javascript angularjs angularjs-directive angularjs-scope

我创建了一个用于创建带搜索功能的下拉列表的指令。 在这个指令中,我使用了一个名为“searchSearchBtn”的标志,基于此我正在显示一个按钮。

好的,让我展示我的代码,一个清晰的想法,

Angular指令

    angObj.directive('largeListSearch', function(domainReports){
            return {
                restrict: 'AE',
                scope:{
                    selectedObj:"=",
                    listColumns: "=",
                    showSearchBtn:"="
                },
                template: '...'+
                    '<input style="width: 350px;" class="dropdown-toggle inactive dd_txt" ng-model="selectedObj.name" id="campaignDropdown" title="{{selectedObj.name}}" placeholder="{{selectedObj.name }}" />'+
                    '<input type="button" value="Search" id="element" style="display: none" data-ng-click="filterDropDown()" />'+
                    '...',
                link: function ($scope, element, attrs) {

$scope.filter = function(val) = {
 //Logic to filter the campaigns
}

                   $scope.originalCampaingList = $scope.$parent.campaigns;

                    //Check the status and load the function accordingly for the campaigns list
                    if(attrs.showSearchBtn === "true") {


                        $scope.filterDropDown = function(){
                            //Function to filter the searched campaign
                            $scope.filter(val);

                        };
                    }else{
                        $scope.$watch('selectedObj.name', function(oldValue, newValue){
                            console.log(oldValue + "  "+ newValue);
                            if(oldValue === newValue) {
                                return;
                            }

                            //Function to filter the searched campaign
                            $scope.filter(val); /HERE Could not able to call the filter function, Is it that, we can not call a function inside $scope.$watch
                            }

                        });
                    }
                }
            }
        });

我的问题:无法调用$ scope中的函数。$ watch

1 个答案:

答案 0 :(得分:1)

当您致电$scope.filter(val)时,当前范围内没有变量val。您需要为val指定一个值。也许你的意思是$scope.filter($scope.selectedObj)