您可以創建可選的一個指令,而無需角UI和與addItem()
方法的範圍添加項目
HTML:
<ol ui-selectable>
JS
var myApp = angular.module('soil', [])
myApp.directive('uiSelectable', function() {
return function (scope, el, attrs) {
el.selectable();
};
});
function ItemCtrl($scope) {
/* triggered by "ng-submit" on a form*/
$scope.addItem = function() {
/* newItem comes from "ng-model" on an input*/
if (!$scope.newItem.length) {
return;
}
$scope.items.push({
name: $scope.newItem
});
$scope.newItem = '';
};
$scope.newItem = '';
$scope.items = [{
name: 'one'
}, {
name: 'two'
}, {
name: 'three'
}];
}
DEMO:http://jsfiddle.net/2KzRt/5/
更新這裏的選擇時,如何創造出一個動態模型集更新列表項:
HTML:
<div id="update_items" ng-show="updateItems.length">
<div ng-repeat="item in updateItems">
<input value="{{item.name}}" ng-model="items[item.index].name"/>
</div>
<button ng-click="updateItems=[]">Cancel</button>
</div>
JS:
var myApp = angular.module('soil', [])
myApp.directive('uiSelectable', function() {
return function (scope, el, attrs) {
el.selectable({
stop:function(evt,ui){
var selected=el.find('.ui-selected').map(function(){
var idx=$(this).index();
return {name: scope.items[idx].name, index:idx}
}).get();
scope.updateItems=selected;
scope.$apply()
}
});
};
});
function ItemCtrl($scope) {
$scope.addItem = function() {
if (!$scope.newItem.length) {
return;
}
$scope.items.push({
name: $scope.newItem
});
$scope.newItem = '';
};
$scope.newItem = '';
$scope.updateItems=[];
$scope.items = [{
name: 'one'
}, {
name: 'two'
}, {
name: 'three'
}];
}
DEMO:http://jsfiddle.net/2KzRt/7/
感謝更新,似乎解決了我的問題。我會將它應用到我的完整應用程序中,以便在接受您的答案之前查看是否有任何我不明白的地方。 – 2013-02-24 16:12:04
正在學習角自己...這是很好的練習,以瞭解如何創建動態模型..希望它適合你 – charlietfl 2013-02-24 16:13:09
它工作得很好。我根據自己的需要調整了它,並添加了一個對話框來詢問所選項目的新名稱:http://jsfiddle.net/SaBaJ/2/ – 2013-02-24 18:28:32