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/