如何从 angular.dart 组件内部调用 jquery 插件? [英] how to call a jquery plugin from inside an angular.dart component?
问题描述
我正在通过尝试制作一个可以访问现有 jquery 插件的组件来了解 angular.dart 组件.我正在尝试类似以下内容:
I am learning about angular.dart components by trying to make one that will access an existing jquery plugin. I am trying something like the following:
library mylib;
import 'dart:html'; // querySelector
import 'package:js/js.dart'; // javascript
import 'package:angular/angular.dart';
@NgComponent(
selector: 'aSelector',
templateUrl: 'partial.html',
cssUrl: 'style.css',
publishAs: 'ctrl',
map: const {
'param': '=>param'
}
)
class myComponent extends NgShadowRootAware {
String param;
Compiler compiler;
Injector injector;
Scope scope;
MyComponent(this.compiler, this.injector, this.scope);
void onShadowRoot(ShadowRoot shadowRoot) {
this.scope.$watch((int) => shadowRoot.querySelector('.myContainer').text.length, (currentValue, previousValue, Scope scope) {
if (currentValue != previousValue) {
var container = context.jQuery('.myContainer', shadowRoot);
var options = map({
'p1': 1,
'p2': 2
});
container.jqplugin(options);
}
});
}
}
不幸的是,容器"似乎是空的......如何让 jQuery 选择一个组件内的元素,即在 shadow dom 中?
Unfortunately, the 'container' seems to be empty... How to make jQuery select an element that is inside a component, i.e. in shadow dom?
顺便说一句,目前推荐的 js 导入方式是什么?我发现了这个:
BTW, what is the currently recomended way to import js? I have found this:
import 'package:js/js.dart' as js;
但是现在包 js 的部分功能被移到 dart:js 中,我不确定应该做什么.
but now that some part of the functionality of package js being moved into dart:js, I'm not sure what one is supposed to do.
推荐答案
问题似乎是 jQuery 问题.也许它适用于:
The problem seems to be a jQuery problem. Perhaps it would work with :
var container = context.jQuery(shadowRoot.querySelector('.myContainer'));
关于 dart:js 与 package:js(参见 你是如何与 dart 中的 js 交互的?) :
About dart:js versus package:js (see How do you interact with js from dart?) :
package:js 提供了一个更简单的 Api,但代价是增加了 js 的大小(因为 package:js 使用了 dart:mirrorsem> 和 noSuchMethod).
package:js provides a simpler Api that comes at the cost of an increase of the js size (because package:js uses dart:mirrors and noSuchMethod).
关于 js 命名空间,您可以随心所欲.我个人更喜欢使用前缀来更好地查看与 Js 的交互何时完成.但这是一个品味问题.
About js namespace you can do what you want. I personnaly prefer to use a prefix to better see when interaction with Js is done. But it's a matter of taste.
这篇关于如何从 angular.dart 组件内部调用 jquery 插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!