【问题标题】:How to implement ng-change for custom directive for select list?如何为选择列表的自定义指令实现 ng-change?
【发布时间】:2015-08-21 18:26:42
【问题描述】:

我的指令使用代码

       <input-select ng-model="someModel" ng-change="someFunction()"
   options="countries"></input-select>

我的指令代码

 .directive('inputSelect', function () {
    return {
        templateUrl: 'someTemplate.html',
        restrict: 'E',
        scope: {
            ngModel: '=',
            ngChange: '='
        }
    };
});

我的指令模板

    <select 
            ng-model="ngModel" ng-init="ngModel "
            ng-options="option[optionId] as option[optionName] for option in options"
            ng-change="ngChange">
    </select>

o,当所选项目更改时,函数 someFunction() @被召唤为无限时间(尽管更改完成了一次),应更改以确保 someFunction() get get on thow @ nofter @ get the @ get the someFunction() 987654326@ 是使用指令的控制器范围内的函数)

[ 我确实尝试将&amp;@ 用于ngChange 的范围类型,如果使用它们,somefunction() 不会被触发。 ]

【问题讨论】:

    标签: javascript angularjs angularjs-directive angularjs-scope ng-options


    【解决方案1】:

    您应该使用 &amp; 用于表达式,并且您可以从标记中调用该方法,例如 ngChange() 而不是 ngChange

    标记

      <select 
            ng-model="ngModel" ng-change="ngChange()"
            ng-options="option[optionId] as option[optionName] for option in options">
      </select>
    

    代码

    scope: {
       ngModel: '=',
       ngChange: '&'
    }
    

    Example Plunkr

    【讨论】:

    • &amp;@ 不起作用 - 如果这样使用,someFunction() 不会被执行。
    • @VishwajeetVatharkar 尝试我建议的方式..根据我的发现更改您的代码..直到那时我会为您创建一个 plunkr
    • 在模板中设置ngChange() 也不起作用。
    • @VishwajeetVatharkar 你能看看我在答案中添加的 plunkr..
    • 你不应该担心ng-change的东西,它应该是ng-change="someFunction(someModel)"而不是ng-change="someFunction(someOtherModel)",因为最终它只是一个参数名称,无论如何它都会提供给控制器将是..查看此plnkr.co/edit/O1RrW7zJldKyuFJMwrid?p=preview
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-19
    • 2019-04-16
    • 1970-01-01
    相关资源
    最近更新 更多