私は私自身の指令を書いているドロップダウンを実装しています。私は入力要素を使用していないので、ngModelを使用していません。カスタム属性で双方向のバインドが可能ですか?NgModelを使用しないカスタムディレクティブでの双方向バインディング。可能?
var mainApp = angular.module('mainApp', []);
mainApp.directive('tableDropdown', ['$timeout',
function($timeout) {
return {
restrict: 'C',
scope: {
selectedFilter: '=?'
},
link: function(scope, elem, attrs) {
$timeout(function() {
angular.element(elem).find('li:first-child').addClass(angular.element(elem).find('li').hasClass('selected') ? '' : 'selected');
scope.selectedFilter.cycleStatus = null;
angular.element(elem).find('li').click(function(e) {
if (angular.element(this).closest('ul').hasClass('active')) {
angular.element(this).closest('ul').removeClass('active');
scope.selectedFilter.selected = angular.element(this).attr('value');
} else {
angular.element(this).closest('ul').addClass('active');
scope.selectedFilter.selected = null;
}
angular.element(this).addClass('selected').siblings().removeClass('selected');
})
}, 0);
}
}
}
])
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<th ng-app="mainApp" ng-init="datefilter.selected=null">
--{{datefilter.selected}}
<ul class="tableDropdown" selected-filter="datefilter.selected">
<li value="null" class="default"><span>Cycle Status</span>
</li>
<li value="completed"><span>Completed</span>
</li>
<li value="cancelled"><span>Cancelled</span>
</li>
</ul>
</th>
私はそれがドロップダウンのように見えるCSSを追加しませんでしたのでご注意ください。私はそれが必要だとは思わなかった。
datefilter.selected
に選択した値を取得してから、別の処理をしたいと考えています。それも可能ですか?何か回避策がない場合は?
現在のコードはそのようなことをしていないと思われるので、双方向バインディングは非常に可能です。ここで達成しようとしているのは、どの値をクリックしてもdatefilter.selectedの変更ですか? – gaurav5430
はい私はそれが価値を変えたいと思う。 'scope.selectedFilter.cycleStatus'から' scope.selectedFilter.selected'にコードを更新しました。 – Roy
副次的なこととして、ディレクティブをクラスに制限するのは一般的には面倒です。 – gyc