如何在Angular Js中编写类的指令? [英] How to write directive on class in Angular Js?

查看:103
本文介绍了如何在Angular Js中编写类的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限制选项通常设置为:

'A' - 仅匹配属性名称

'E' - 仅匹配元素名称< br>
'C' - 仅匹配类名

'M' - 仅匹配评论

'A' - only matches attribute name
'E' - only matches element name
'C' - only matches class name
'M' - only matches comment

'C' - 仅匹配类名称不起作用

'C' - only matches class name is not working

Class =form-control ** valid-vehicleyear ** ng-not-empty ng-dirty ng-valid-解析ng-valid ng-valid-required-required ng-touching

我在与element相关的类上创建了一个指令。在价值变化时,我想调用api并改变其他元素的值。但是没有观察到变化。

I created a directive on class associated with element. On change of value i want to call a api and change value of other element. But no change is observed on change.

controlDirective.js

controlDirective.js

  function validVehicleyear($scope, $http) {
        return {
            restrict: 'C',
            scope: {
                ngModel: '=',                
            },
            link: function (scope, element, attrs, ngModel) {
                element.bind('change', function () {
                    console.log('here in validVehicleyear');
                    $http.get('api.php'+scope.ngModel)
                            .then(function (response) {
                                $scope.answers.VehicleMake = response.data;
                            });
                });
            }
        }
    }

车辆年问题有课有效-vehicleyear。我在这里缺少什么,或者在更改答案时还有其他任何内容。
我在车辆年问题上写了一个指令validVehicleyear,这个我想调用年份更改并为Vehicle make设置新选项,但它不起作用。

Vehicle year question has a class valid-vehicleyear. what I am missing here, or is there any other to this on change of answers.vehicleyear. I wrote a directive validVehicleyear on class at Vehicle year question, this i want to call on change of year and set new options for Vehicle make, but it not working.

plnkr.co/edit/BFGXr7LNAe0KvQipj9JJ?p=preview

我查了一下,发现外/内指令概念可以在这里工作。但没有得到如何申请动态类。

I checked around and found that outer/inner directive concept can work here. but not getting how to apply for the dynamic classes.

推荐答案

返回你的另一个问题,我尝试了几件事,其中没有一个因为这个原因:

Back from your other question, I tried a couple of things none of which worked for this reason:

您将指令作为类传递,但是通过插值动态传递,这本身就是不好的做法( https://docs.angularjs.org/guide/interpolation#known-issues )。类名是插值的并且元素被渲染但是在插值期间没有编译指令。

You pass a directive as a class but dynamically by interpolation which in itself is bad practice (https://docs.angularjs.org/guide/interpolation#known-issues). The class name is interpolated and the element rendered but the directive is not compiled during interpolation.

唯一有效的方法是将指令名称传递给clear:

The only thing that worked was to pass the directive name in clear:

class="form-control valid-vehicleyear"

然后你所有的选择元素都会有这个类/指令。

But then all your select elements will have this class/directive.

你正试图自动化所有东西而你正在推动极端的概念使你的代码非常难以理解并且显然无法调试。

You're trying to automate everything and you're pushing the concept to the extreme which makes your code very unreadable and apparently impossible to debug.

按元素构建表单元素并在每个表单上放置自定义指令没有任何问题为了更好的控制。

There's nothing wrong with building a form element by element and putting custom directives on each of them for better control.

然而,将动态指令作为JSON对象中的类传递会出现问题。

However there is everything wrong with passing dynamic directives as classes from a JSON object.

只需正常构建表单。它不会太酷或不太可读,它将遵循最佳实践( https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#restrict-to-elements-and-attributes

Just build your form normally. It won't be less cool or less readable and it will follow best practice (https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#restrict-to-elements-and-attributes)

<select valid-vehicleyear>
<select valid-makemodel>
...

这篇关于如何在Angular Js中编写类的指令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆