[英]AngularJS $rootScope function ng-change
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()
返回。
如何from the
$ rootScope`運行ng-change
或ng-click?
ng-change
接受參數,它是一個表達式。 輸入值更改后,將在當前作用域的上下文中評估表達式。 這意味着,如果與當前元素關聯的范圍(或其任何其他父范圍)沒有$rootScope
屬性,則您的代碼段將無法工作。
嘗試這樣的事情:
<input type="text" data-search="type" placeholder="Search a Candidate, Job, Certificate or Location" ng-model="searchMain" ng-change="updateSearchField()"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.