AngularJs 指令元素 children 为空

AngularJs Directive element children gives empty

我正在尝试获取选项中所选节点的 innerHTML

<i class="before"></i> 标签应该从同一组页面加载的选定或第一个选项接收文本。但是只有硬编码的下拉菜单才能在加载时获得价值,但是 ngRepeat 的下拉菜单什么也得不到。

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function($scope) {
  
    $scope.investments = [
      {"name": "AARP Operating Funds"},
      {"name": "Some Big Title"},
      {"name": "I hatez IE8"},
      {"name": "I Love FF DeveLoper Edition"},
      {"name": "Give Me Some Sunshine"}
    ];

    $scope.investment = $scope.investments[0].name;


  })
  .directive('dropdownSelectBox', function(){


    return function(scope, element, attrs) {

      //.filter('option[value="' +  element.find('select').val() + '"]').text()

      console.log(element.find('select').children());

      if (element.find('select').val() == '') {
        element.find('i').text($('select').eq(0).text());
      } else {
        element.find('i').text(element.find('select').children()[0].innerHTML);
      }

      element.find('select').bind('change', function(){                    
          element.find('i').text($(this).find(':selected').text());
      });
      
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myCtrl">

  <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select id="timePeriod">
          <option value="7" selected="selected">Last 7 Days </option>
          <option value="30">Last 30 Days </option>
          <option value="60">Last 60 Days </option>
          <option value="current">Current Year </option>
          <option value="prior">Prior Year</option>
      </select>

    </span>


    <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select ng-options="opt.name as opt.name for opt in investments | orderBy:'name'" ng-model="investment"></select>

    </span>

</div>

最好用ng-model:

var myapp = angular.module('myapp', []);
myapp.controller('myCtrl', function($scope) {
  
    $scope.investments = [
      {"name": "AARP Operating Funds"},
      {"name": "Some Big Title"},
      {"name": "I hatez IE8"},
      {"name": "I Love FF DeveLoper Edition"},
      {"name": "Give Me Some Sunshine"}
    ];

    $scope.investment = $scope.investments[0];
    $scope.period = 'Last 7 Days';

  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="myCtrl">

  <span class="dropdown" dropdown-select-box="">
      
      <i class='before'>{{period}} {{investment.name}}</i>
      <select id="timePeriod" ng-model="period">
          <option value="Last 7 Days" selected="selected">Last 7 Days </option>
          <option value="Last 30 Days">Last 30 Days </option>
          <option value="Last 60 Days">Last 60 Days </option>
          <option value="Current Year">Current Year </option>
          <option value="Prior Year">Prior Year</option>
      </select>

    </span>


    <span class="dropdown" dropdown-select-box="">
      
      <i class='before'></i>
      <select ng-options="opt.name for opt in investments | orderBy:'name'" ng-model="investment"></select>

    </span>

</div>

为了能够操纵 'element',我刚刚将 jquery 脚本 link 移动到 angularjs.

之前

演示:http://runnable.com/VK5RPx_xRDp8aqF_/angularjs-crossbrowser-select-dropdown-for-jquery-and-javascript