AngularJS$rootScope函数ng更改

AngularJS $rootScope function ng-change

本文关键字:ng 更改 函数 rootScope AngularJS      更新时间:2023-09-26

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我使用路由来加载页面。

角度

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或单击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()"/>