javascript - AngularJS $rootScope 函数 ng-change

标签 javascript angularjs angularjs-scope

html

<input type="text" data-search="type" placeholder="Search a Candidate, Job, Certificate or Location" ng-model="searchMain" ng-change="$root.updateSearchField(searchMain)"/>

上面是一个用作实时搜索的输入字段,将其放入 index.html并使用 Angular-Route我正在使用路由来加载页面。

Angular

app.run(function($rootScope, $templateCache) {

        $rootScope.$on('$routeChangeStart', function(event, next, current) {
            console.log(current);
            if (typeof(current) !== 'undefined'){
                $templateCache.remove(current.templateUrl);
            }
        });

        $scope.filterText = '';
        // Instantiate these variables outside the watch
        var tempFilterText = '', filterTextTimeout;
        $rootScope.updateSearchField = function(foo) {
            console.log($scope.searchMain);
            if (filterTextTimeout) $timeout.cancel(filterTextTimeout);
            tempFilterText = foo;
            filterTextTimeout = $timeout(function() {
                $rootScope.searchText = tempFilterText;
                console.log($rootScope.searchText);
            }, 250); // delay 250 ms
        };

    });

ng-change() 的功能我看过应该在.run()里面然而当我按下键盘时仍然没有 console.log()返回。

我怎样才能运行 ng-change或 ng-click from the $rootScope`?

最佳答案

ng-change 接受一个参数,它是一个表达式。输入值更改后,将在当前范围的上下文中评估表达式。这意味着,如果与当前元素(或其任何其他父范围)关联的范围没有 $rootScope 属性,则您的代码段无法工作.

尝试这样的事情:

<input type="text" data-search="type" placeholder="Search a Candidate, Job, Certificate or Location" ng-model="searchMain" ng-change="updateSearchField()"/>

关于javascript - AngularJS $rootScope 函数 ng-change,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25162175/

相关文章:

javascript - 谷歌美化 bool 查询语言

javascript - 当它被拖到另一个 div 附近时淡出 div

javascript - 当日期选择器和相邻元素之间没有空格时,Angular-Datepicker 不起作用?

javascript - 循环数组Javascript后替换对象属性值中的字符串

javascript - Angular ng-view 或 ng-include 不起作用

html - 为什么最新版本的angular不支持全局 Controller 功能?

javascript - 服务器端请求和 XmlHTTPRequest(客户端)和安全性

JavaScript 内存泄漏 - DOM 节点和后代上的垃圾收集

javascript - AngularJS:如何在另一个 Controller 中执行方法?

angularjs - 调试 Angular 代码的最佳方法是什么?