在多选中设置值isteven of angular js

时间:2015-10-01 05:56:58

标签: angularjs angularjs-directive multi-select

我试图将Angularjs多选项用于我的项目。

以下html是我的多选div。

<div     
                multi-select
                input-model="marks"
                output-model="filters.marks"
                button-label="name"
                item-label="name"
                tick-property="ticked"
                selection-mode="multiple"
                helper-elements="all none filter"
                on-item-click="fClick( data )"
                default-label="Select marks"
                max-labels="1"
                max-height="250px"

            >
            </div>

我知道我可以在控制器中使用$ scope.marks = data。

但问题是$ scope.marks是一个我无法改变的全局变量。

有没有办法在不使用输入模型的情况下在多选中设置值?

2 个答案:

答案 0 :(得分:2)

嗯,在这里做一些测试,我可以得到多选的东西:

var languages = ["C#", "Java", "Ruby", "Go", "C++", "Pascal", "Assembly"]; //The items.

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

myApp.controller('MyCtrl', function($scope) {
  $scope.marks = {};
  for (lang in languages) {
    $scope.marks[lang] = {
      name: languages[lang],
      marked: false
    };
  }

  $scope.marks[3].marked = true; //mark "Go" and "C++" by default.
  $scope.marks[4].marked = true;

  $scope.theMarkedOnes = function() {
    outp = "";
    for (m in $scope.marks) {
      if ($scope.marks[m].marked)
        outp += $scope.marks[m].name + ", ";
    }
    if (outp.length == 0) {
      return "(none)";
    } else {
      return outp.substr(0, outp.length - 2);
    }
  }
  $scope.setMark = function(markone) {
    markone.marked = !markone.marked;
  }

})
*,
*:before,
*:after {
  box-sizing: border-box;
}
body {
  font-family: sans-serif;
  font-size: 0.7em;
}
::-webkit-scrollbar {
  width: 7px;
}
::-webkit-scrollbar-track {
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.3);
  border-radius: 10px;
}
::-webkit-scrollbar-thumb {
  border-radius: 10px;
  -webkit-box-shadow: inset 0 0 6px rgba(0, 0, 0, 0.5);
}
.multiselector {
  background-color: #CCCCCC;
  overflow-y: scroll;
  width: 17em;
  height: 13em;
  border-radius: 0.7em;
}
.multiselector .item {
  cursor: pointer;
  padding: 0.2em 0.3em 0.2em 0.0em;
}
.itemtrue {
  background-color: #9999AA;
}
.msshow {
  background-color: #cccccc;
  border-radius: 0.7em;
  margin-top: 1em;
  padding: 0.6em;
  width: 17em;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
  <div class="multiselector">
    <div ng-repeat="mark in marks" class="item item{{mark.marked}}" ng-click="setMark(mark)">{{mark.name}}</div>
  </div>

  <div class="msshow"> <b>Selected:</b> {{theMarkedOnes()}}</div>
</div>

答案 1 :(得分:1)

设置并获取Angularjs isteven-multi-select的选定值,名称和文本

<div isteven-multi-select
    input-model="marks"
    output-model="filters.marks"
    button-label="name"
    item-label="name"
    tick-property="ticked"
    selection-mode="multiple"
    helper-elements="all none filter"
    on-item-click="fClick( data )"
    default-label="Select marks"
    max-labels="1"
    max-height="250px">
</div>

添加项目

$scope.marks= [
    { name: 'Mark I', value: 'Mark i', text: 'This is Mark 1', ticked: true },
    { name: 'Mark II', value: 'Mark ii', text: 'This is Mark 2' },
    { name: 'Mark III', value: 'Mark iii', text: 'This is Mark 3' }
];

获取所选项目(更改时)

$scope.fClick = function (data) {
    console.log(data.name);
    console.log(data.value);
    console.log(data.text);
    return;
}

选择项目(带有代码)

$scope.abc = function (data) {
    console.log(data.element1, data.element2);

    angular.forEach($scope.marks, function (item) {
        if (item.value == data.element1) {
            item.ticked = true;
        }
        else {
            item.ticked = false;
        }
    });
}

取消选择项目(清除)

$scope.ClearClick = function () {
    $scope.Filter = { selectMarks: 'Mark i' };

    $scope.marks.map(function (item) {
        if ($scope.Filter.selectMarks == item.value)
            item.ticked = true;
        else
            item.ticked = false;
    });
}