javascript - 在 $scope.$watch 中调用一个函数

标签 javascript angularjs angularjs-directive angularjs-scope

我已经创建了一个用于创建具有搜索功能的下拉菜单的指令。 在此指令中,我使用了一个名为“searchSearchBtn”的标志,基于此我显示了一个按钮。

好吧,让我展示一下我的代码,为了一个清晰的思路,

Angular Directive(指令)

    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 中的函数

最佳答案

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

关于javascript - 在 $scope.$watch 中调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27084763/

相关文章:

javascript - 如何解析 AngularJS 中的 $resource 对象

javascript - 为什么我在 AngularJs 中的指令不起作用?

angularjs - 自定义指令未输出正确的结果

javascript - 文本颜色的随机色调不起作用

javascript - 如何配置 create-react-app 以创建具有类而非函数的应用程序?

javascript - 带有分页 : no rows displayed 的 AngularJS ng 表

AngularJS 指令在元素完全加载之前运行

javascript - Web Crawler - 返回要在下一个函数中使用的数组

javascript - vue合并两个对象数组

javascript - Angular 中的范围变量和 ng-include 不协调