【问题标题】:Selected item in directive not working指令中的选定项目不起作用
【发布时间】:2016-03-22 13:13:34
【问题描述】:

我创建了一个select directive,并且两次使用了这个指令。我需要查看两者的选定项目。我该怎么办?

HTML

<div select-list="items"></div>
<div select-list="items2"></div>

控制器

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

myApp.controller('mainController', function($scope) {
    $scope.items = [
        {
            name: "1"
        },
        {
            name: "2"
        }
    ];

    $scope.items2 = [
        {
            name: "3"
        },
        {
            name:"4"
        }
    ];

    $scope.selectedValues = [];
});

选择指令

myApp.directive("selectList", function() {
    return {
        restrict: "EACM",
        template: '<select ng-model="selectedValues" ng-options="item.name for item in data"></select>',
        scope: {
            data: '=selectList'
        }
    }
});

我需要将两个“选择”的选定项目添加到 $scope.selectedValues 中。 我尝试了ng-change,但没有成功。

【问题讨论】:

    标签: angularjs drop-down-menu angularjs-directive angular-ngmodel


    【解决方案1】:

    您的指令使用隔离范围,因此您无法从控制器访问指令或从指令访问控制器。

    您必须创建一个新条目。

    我让你一个正在工作的小提琴:

    https://jsfiddle.net/Lv1q2sh2/1/

    // Code goes here
    var myApp = angular.module('app', []);
    angular.module('app')
    .directive("selectList", function(){
      return {
          restrict: "EACM",
          require: 'ngModel',
          template: '<select ng-model="selected" ng-change="onSelectedValue()" ng-options="item.name for item in data"></select>',
          scope: {
              data: '=selectList'
          },
          link: function (scope, element, attr, ngModel) {
            scope.onSelectedValue = function () {
                ngModel.$setViewValue(scope.selected);
            }
          }
      }
    })
    .controller('mainController', function($scope) {
      $scope.items = [
          {name: "1"},
          {name: "2"}
      ];
      $scope.items2 = [
          {name:"3"},
          {name:"4"}
      ];
      $scope.selectedValues = [];
    });
    

    【讨论】:

      【解决方案2】:

      需要正确创建指令:

      1. 为您的指令设置一个控制器
      2. 如果您使用隔离范围,请确保将 selectedValue 传递给范围。 例如:

      指令:

      myApp.directive("selectList", function(){
      return{
          restrict: "EACM",
          template: '<select ng-model="selectedValues" ng-options="item.name for item in data"></select>',
          scope: {
              data: '=selectList',
              ngModel: '='
          }
          //Add link function here, crate watcher on ngModel and update it back on select dropdown selection.
      })};
      

      HTML:

      <div select-list="items" ng-model="selectedValue1" ></div>
      <div select-list="items2" ng-model="selectedValue2"></div>
      

      在指令中添加链接功能并在 ngModel 上放置一个观察者,一旦用户更改选择,更新父 ng-model。

      【讨论】:

      • 对不起,你能再详细点吗?
      • 做了一些修改来回答,希望这能澄清你的疑惑。
      猜你喜欢
      • 2012-07-09
      • 2013-07-01
      • 2013-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多