Angularjs 角度数据自定义过滤器

Angularjs 角度数据自定义过滤器,angularjs,filter,Angularjs,Filter,如何在代码中包含自定义过滤器 这是我的服务文件。我需要按名称筛选。此外,我还需要在html中使用pristine对save和cancel进行验证 app.factory('CrusdService',函数($http){ 返回{ fetchAll:function(){ 返回$http.get('https:\\localHost:5000\countries')。然后( 功能(响应){ return response.data.data;//取决于响应数据。有时是response.data.d

如何在代码中包含自定义过滤器

这是我的服务文件。我需要按名称筛选。此外,我还需要在html中使用pristine对save和cancel进行验证

app.factory('CrusdService',函数($http){
返回{
fetchAll:function(){
返回$http.get('https:\\localHost:5000\countries')。然后(
功能(响应){
return response.data.data;//取决于响应数据。有时是response.data.data.data
},
函数(错误){
返回误差;
}
);
},
添加:功能(数据){
返回$http.post('https:\\localHost:5000\country',数据)。然后(
功能(响应){
返回响应;
},
函数(错误){
console.log('error');
}
);
},
更新:功能(数据){
变量名称={
“名称”:data.name
};
返回$http.put('https:\\localHost:5000\country'+data.\u id,name)。然后(
功能(响应){
返回响应;
},
函数(错误){
console.log('error');
}
);
},
激活:功能(id){
返回$http.put('https:\\localHost:5000\country'+id+'\activate')。然后(
功能(响应){
返回响应;
},
函数(错误){
console.log('error');
}
);
},
停用:功能(id){
返回$http.put('https:\\localHost:5000\country'+id+'\deactivate')。然后(
功能(响应){
返回响应;
},
函数(错误){
console.log('error');
}
);
}
}
});
在html文件中添加以下内容:
对于搜索字段,输入ng model=“searchValue”
In ng repeat=“国家/地区中的数据|.|过滤器:搜索值”
app.filter(“customSearch”,函数(){
返回函数(数据、搜索){
var筛选=[];
如果(!!搜索){
对于html文件中的(var i=0;i
),添加以下内容:
对于搜索字段,输入ng model=“searchValue”
In ng repeat=“国家/地区中的数据|.|过滤器:搜索值”
app.filter(“customSearch”,函数(){
返回函数(数据、搜索){
var筛选=[];
如果(!!搜索){
对于(var i=0;i
$scope.search=函数(searchValue){
$scope.list=($filter(“filter”)($scope.searchList,{name:searchValue}));
};
HTML


要求的

function countryList(){CrudeService.fecthal().then(function(data){$scope.countries=data;},function(data){console.log('error');})}countryList();CrudeService.add($scope.country());然后(function(data){countryList();},function(data){console.log('error');});CrudeService.update($scope.country.);然后(function(data){countryList();},function(data){console.log('error');});CrudeService.activate(itemsId)。然后(函数(数据){countryList();},函数(数据){console.log('error');});CrudeService.deactivate(itemsId)。然后(函数(数据){countryList();},函数(数据){console.log('error');});不要在注释中添加代码,你的问题。
in html file add the following content:

For the search field give the ng-model="searchValue"
In ng-repeat = "data in country |.. | filter:searchValue"

app.filter("customSearch",function(){
  return function(data,search){
    var filtered = [];
            if(!!search){
              for(var i=0;i<data.length;i++){
               if(data[i].country.toLowerCase().indexOf(search) !== -1){
                  filtered.push(data[i]);
               }
              }
            }
            else{
             filtered = data;
            }
            return filtered
  }
})
In html file add the following content:

For the search field give the ng-model="searchValue"
In ng-repeat = "data in country |.. | filter:searchValue"

app.filter("customSearch",function(){
  return function(data,search){
    var filtered = [];
            if(!!search){
              for(var i=0;i<data.length;i++){
               if(data[i].country.toLowerCase().indexOf(search) !== -1){
                  filtered.push(data[i]);
               }
              }
            }
            else{
             filtered = data;
            }
            return filtered
  }
})


in html file add the following content:
<span class="error" ng-if="formname.inputname.$invalid">enter correct data</span> //add this below the save button
for disbaling save and update button in pop up

save    - ng-disabled="formname.inputname.$invalid || formname.inputname.$pristine"  //formname is the name of the form and inputname is name of the button. if button name is not there add one.
update  - ng-disabled="formname.inputname.$invalid || formname.inputname.$pristine"
-----
 ng-model="searchString" ng-change="search(searchString)">
 $scope.search = function(searchValue) {
            $scope.list = ($filter("filter")($scope.searchList, {name: searchValue}));
        };
  <div class="form-group" ng-class="{'has-error': dataForm.country.$invalid && dataForm.country.$dirty}">
                    <input type="text" ng-model="country" name="country" class="form-control" required>
                    <span class="error text-danger" ng-show="dataForm.country.$invalid && dataForm.country.$dirty">Required</span>
                    <input type="submit" value="Submit" ng-disabled="dataForm.$invalid">
                </div>