簡體   English   中英

angularjs $ scope.watch文本字段錯誤?

[英]angularjs $scope.watch text field error?

我已經使用本地存儲過濾列表頁面。當我清除按鈕時,我有一個帶有清除按鈕的文本框,因為該$ scope.watch。如果我在文本框中輸入值,那么$ scope.watch也不允許該值如何當我單擊清除按鈕時避免此錯誤。當我清除或進行ng-model更改時,我需要根據該值列表選擇當前值

 .controller('ListingCtrl', [ '$scope', '$http', '$location', '$window', '$filter','$ionicPopover','$ionicLoading', function($scope, $http, $location, $window, $filter, $ionicPopover, $ionicLoading) { $scope.$watch(function() { var searchstore =window.localStorage.getItem("searchstore"); console.log(searchstore); $scope.query=searchstore; }) $scope.clearSearch = function() { $scope.query = ''; }; $http.get('*****').success(function(data,dealers,response) { $scope.dealers = data; }); }]) <!-- begin snippet: js hide: false --> 
 <div class="bar bar-header item-input-inset"> <label class="item-input-wrapper" > <i class="icon ion-ios-search placeholder-icon"></i> <input type="text" placeholder="Search" ng-model="query" > </label> <button class="button button-icon ion-ios-close-outline" ng-click="clearSearch()" id="iconcolor" >clear</button> </div> <div class="list card" data-ng-init="nearme()" data-ng-repeat="dealer in dealers | filter:query "> <div class="item item-thumbnail-left item-icon-right" href="#"> <h2>{{dealer.Store_Name}}</h2> <p>{{dealer.S_Address.area}} {{dealer.S_Address.city}}</p> <p>{{dealer.S_Services}}</p> </div> </div> 

這應該像您想要的那樣工作。

$scope.$watch(function() {
  return window.localStorage.getItem("searchstore")
},
function(searchstore) {
  $scope.query = searchstore;
});

$scope.clearSearch = function() {
  // You could remove aswell, but I prefer setting it empty
  // window.localStorage.removeItem("searchstore");
  window.localStorage.setItem("searchstore", "");
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM