如何在Angular Js中编写类的指令?

编程入门 行业动态 更新时间:2024-10-26 17:28:01
本文介绍了如何在Angular Js中编写类的指令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

限制选项通常设置为:

'A' - 仅匹配属性名称 'E' - 仅匹配元素名称 '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:

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

You pass a directive as a class but dynamically by interpolation which in itself is bad practice (docs.angularjs/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.

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

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

更多推荐

如何在Angular Js中编写类的指令?

本文发布于:2023-11-13 11:59:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指令   如何在   Angular   Js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!