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