1

基本上,我有一个指令(称之为站点,即位置),它有很多用于收集数据的输入。页面上可能有数百个,因此我有一个搜索输入可以快速找到特定的网站。当父母被过滤时,输入正在丢失他们的值

我的问题是,无论何时使用搜索,筛选出的项目都会丢失为其输入的所有数据。我期望过滤的项目“隐藏”并保持它们的值,但它好像是被重新渲染的。我做错了什么,或者我如何才能以正确的方式完成这项工作?

Here's a very simple Plunker of my issue

的HTML:

<body ng-controller="MainCtrl"> 
    <input type="search" ng-model="q" placeholder="filter sites..."> 
    <my-directive ng-repeat="site in sites | filter:q" site="site"></my-directive> 
</body> 

脚本:

var app = angular.module('plunker', []); 

app.controller('MainCtrl', function($scope) { 
    $scope.sites = [ 
    {name: 'site 1', id:'site1'}, 
    {name: 'site 2', id:'site2'} 
    ]; 
}); 

app.directive('myDirective', function($compile) { 
    return { 
    restrict: 'E', 
    scope: { 
     site: '=', 
    }, 
    template: '<h1>{{site.name}}</h1>Value: <input name="{{site.id}}">' 
    }; 
}); 
+0

你清爽每次搜索时都会重复ng-repeat,从而在输入中没有任何数据的情况下重新创建指令。 – 2015-03-02 16:53:15

回答

1

没有什么约束力你的输入值模型。当角重绘创建新元素的UI,所以任何以前输入的不是存储,而无需使用ng-model(或事件)来存储值

试试这个:

template: '<h1>{{site.name}}</h1>Value: <input name="{{site.id}}" ng-model="site.SomeProperty">' 

updated plunker