AngularJs指令元素子元素为空

时间:2015-01-06 11:46:38

标签: javascript angularjs angular-directive

我试图在选项

上获取所选节点的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>

2 个答案:

答案 0 :(得分:0)

最好使用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>

答案 1 :(得分:0)

为了能够操作'element',我在angularjs之前移动了jquery脚本链接。

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