angularjs ngclick清除并搜索

时间:2015-12-18 21:57:06

标签: javascript angularjs angularjs-ng-repeat clear angularjs-ng-click

试图解决一个看起来非常简单但难以理解的问题: - )

我有Json文件,我试图使用AngularJS获取数据,基于用户输入(名称),浏览器只显示那些结果而不是旧结果。

names.json

- (void) getAllStoredDataForHKQuantityIdentifier:(NSString *)theHKIdentifier withCompletion:(void(^)(NSArray *results))completion startingFromAnchor: (HKQueryAnchor *)anchor {

    HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:theHKIdentifier];

    HKSourceQuery *sourceQuery = [[HKSourceQuery alloc] initWithSampleType:quantityType samplePredicate:nil completionHandler:^(HKSourceQuery *query, NSSet *sources, NSError *error) {
        if( error ) {
            completion(nil);
            return;
        }

        NSMutableSet *sourcesNo = [NSMutableSet new];
        for( HKSource *thisSource in sources ) {
            if( ![thisSource.bundleIdentifier isEqualToString:@"my.appname.app"] ) {
                [sourcesNo addObject:thisSource];
            }
        }

        for(HKSource* useSource in sourcesNo){

            NSPredicate *predicate = [HKQuery predicateForObjectsFromSources:[NSSet setWithObject:useSource]];

            HKAnchoredObjectQuery *newQuery = [[HKAnchoredObjectQuery alloc] initWithType:quantityType predicate:predicate anchor:anchor limit:HKObjectQueryNoLimit resultsHandler:^(HKAnchoredObjectQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable sampleObjects, NSArray<HKDeletedObject *> * _Nullable deletedObjects, HKQueryAnchor * _Nullable newAnchor, NSError * _Nullable error) {
                completion(sampleObjects);
            }];

            [healthStore executeQuery:newQuery];
        }
    }];

    [healthStore executeQuery:sourceQuery];
}

实施例: 输入:艾伦

输出:姓名:Alan,班级:1,书籍:20。

我已设法使用以下代码打印预期结果:

names.js文件:

{"name": {
  "Alan": [{ "Class":"1", "Books":"20"}],
  "Sam": [{ "Class":"2", "Books":"204"}],
  "John": [{ "Class":"4", "Books":"240"}],
   }
 }

我的HTML文件如下所示:

var NamesApp = angular.module('NamesApp', [])
NamesApp.controller('NameCtrl', function($scope, $http){
$http.get('names.json').success(function(data) {
          $scope.getit = data;
        });
        $scope.results = [];
        $scope.findValue = function(userInput) { 
        angular.forEach($scope.getit.name, function(value, key) {
      if (key === userInput) {
        $scope.results.push({n: key, c: value[0].Class, b: value[0].Books});
      }

    });

    };
});

问题: 当您第一次搜索或刷新页面后,您将获得干净的结果。

在搜索其他名称之前,我应该如何清理搜索结果?

“我的结果”部分应始终只显示查询结果,而不是旧结果。

1 个答案:

答案 0 :(得分:3)

在每次搜索前重置$scope.results

$scope.findValue = function(userInput) { 
        $scope.results = [];
        angular.forEach($scope.getit.name, function(value, key) {
      if (key === userInput) {
        $scope.results.push({n: key, c: value[0].Class, b: value[0].Books});
      }
    });
}