【发布时间】:2016-10-18 03:21:04
【问题描述】:
我有一个 div 控制器,其中一个 div 使用 ng-init 在控制器内部的一个数组进行了初始化。我需要使用文本框中的文本作为过滤器来显示该数组内的过滤对象。我还需要在用户输入输入时显示对象。这是我的基本代码:
<!DOCTYPE html>
<html lang="en-US">
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
<div style="display:none" ng-init="CustList=[
{custid:1,custname:'Raj Export House1',address:'Plot.No.123, Industrial Area, Noida 201301'},
{custid:2,custname:'Vinay Export House2',address:'Plot.No.567, Industrial Area, New Delhi 201301'},
{custid:3,custname:'Maya Export House3',address:'Plot.No.777, Industrial Area, Faridabad 201301'},
{custid:4,custname:'Devat Export House4',address:'Plot.No.425, Industrial Area, Gurgaon201301'},
{custid:5,custname:'Yespal Export House5',address:'Plot.No.153, Industrial Area, Noida 201301'},
{custid:6,custname:'Abhinav Export House6',address:'Plot.No.1423, Industrial Area, Noida 201301'},
{custid:7,custname:'Surya Export House7',address:'Plot.No.1253, Industrial Area, Noida 201301'},
{custid:8,custname:'Lalata Export House8',address:'Plot.No.12553, Industrial Area, Gurgaon 201301'},
{custid:9,custname:'Raj Export House9',address:'Plot.No.12553, Industrial Area, Noida 201301'},
{custid:10,custname:'Manu Export House10',address:'Plot.No.15823, Industrial Area, Noida 201301'}]"></div>
<label>Search</label>
<input type="text" ng-model="txtSearch" ng-change="CallSearch(txtSearch)">
<ul>
<li style="display:none" ng-repeat="cust in CustList | filter:txtSearch">
{{ cust.custid + ' ' + cust.custname + ' ' + cust.address + ',' }}
</li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.CallSearch = function(textSearch)
{
if(textSearch.length>0);
$("li").css("display","block");
}
});
</script>
我也尝试使用ng-repeat="cust in CustListfunction() 并在作用域内定义了这个函数
$scope.CustListfunction=function(){
var result=[];
angular.forEach($scope.CustList, function (item) {
if($scope.textSearch.length>0)
{
result.push(item);
}
});
return result;
}
但它没有让我到任何地方,有什么我想念的吗? 谢谢
【问题讨论】:
标签: angularjs arrays angularjs-ng-repeat ng-repeat