【问题标题】:Calling custom directive form controller调用自定义指令表单控制器
【发布时间】:2015-04-30 18:53:50
【问题描述】:

我编写了一个自定义指令来加载 3 个下拉菜单并处理控制器内下拉菜单的更改事件。在更改下拉值时,我在控制器函数中获得了更改后的值。我需要调用我的自定义指令并传递新值。

我是 AngularJs 自定义指令的新手。请告诉我如何从控制器中调用指令。

我呈现自定义指令的视图:

<div class="container">
  <div class="main-header clearfix">
    <div class="page-title">
        <h3 class="no-margin">Search for a Host</h3>
    </div>
    <!-- /page-title -->
  </div>
  <div class="padding-md">
    <div class="hostsFilters row">
        <select ng-model="BU" ng-change="changeTheView()" class="form- control">
            <option value="">BU</option>
            <option value="1">BU1</option>
            <option value="2">BU2</option>
            <option value="3">BU3</option>
            <option value="4">BU4</option>
        </select>
        <span>OR</span>
        <select ng-model="Application" ng-change="changeTheView()" class="form-control">
            <option value="">Application</option>
            <option value="1">App1</option>
            <option value="2">App2</option>
            <option value="3">App3</option>
            <option value="4">App4</option>
        </select>
        <span>OR</span>
        <input type="text" class="form-text" ng-model="hostName.host_name" placeholder="Hostname">
    </div>
</div>
<div class="row">
    <i-data-grid></i-data-grid>
</div>

我的控制器的代码:

'use strict';

angular.module('angularFullstackApp')
  .controller('MainCtrl', function ($scope) {
  $scope.changeTheView=function(){
    console.log('Came inside the Change the view function..');
    //Now invoke the custom directive.
  }
});

我的指令的代码:

'use strict';
angular.module('angularFullstackApp')
  .directive('iDataGrid', function () {
    return {
      templateUrl: 'app/iDataGrid/iDataGrid.html',
      restrict: 'E',
      link: function (scope, element, attrs) {
        $(document).ready(function() {
        $('#example').dataTable();
      } );
    }
  };
});

【问题讨论】:

  • 你能添加一些你试过的代码吗..
  • 你想从 iDataGrid 本身调用这些事件还是通过创建不同的指令来调用这些事件?
  • 当下拉列表有任何变化时,需要从Controller调用iDataGrid。控制器不断监听下拉列表中的变化,在有变化的地方,控制器需要调用指令并将变化的值传递给它。

标签: angularjs angularjs-directive


【解决方案1】:

您可以使用属性,并在属性内传递值,您想为其放置一个在任何值更改时都会触发的手表

标记

<i-data-grid watch-values="['BU', 'Application']"></i-data-grid>

代码

'use strict';

angular.module('angularFullstackApp')
  .directive('iDataGrid', function () {
    return {
      templateUrl: 'app/iDataGrid/iDataGrid.html',
      restrict: 'E',
      link: function (scope, element, attrs) {
        $(document).ready(function() {
        $('#example').dataTable();
        scope.$watch(attrs.watchValues,function(newVal, oldVal){
            //newly changed value available here with same sequence
            //as you passed ['BU', 'Application'] here, it fires fn when any of value changed,
            //newVal[0] contains newly changed value of BU
            //& newVal[1] will contain newly changed value of Application
            //call what ever code on basis of this values
        },true);
      });
    }
  };
});

【讨论】:

  • 谢谢。但是我将如何获得 watch-values 标签的更改值?我理解指令部分,但不理解调用部分。
  • 你能ping我你的gmail id吗?这样沟通会更好吗?
  • 那么就不用调用控制器的changeTheView函数了吗?
  • @Pradeep 你可以摆脱那个方法,你可以在我的 gmail parkarpankaj3@gmail.com 上 ping 我
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2016-09-08
  • 2016-08-06
  • 1970-01-01
相关资源
最近更新 更多