简体   繁体   English

svg和angularjs-一个简单的ngClick示例

[英]svg and angularjs - a simple ngClick example

I have a simple problem (I hope). 我有一个简单的问题(我希望)。 I would like to bind so to say svg element and angularjs. 我想绑定这样说svg元素和angularjs。 Here how the problem looks like: 这里的问题看起来是这样的:

<svg> <g ...> <text ... ng-click="count=count+1" ng-init="count=0">Click me!</text></g></svg>

outside <svg> I have the following code: <svg>之外,我有以下代码:

count: {{count}}

I would like to clik on "Click me!" 我想点击“点击我!” and see how many clicks did I make. 并查看我获得了多少点击。 That does not work. 那行不通。 I see no result, my question is why? 我没有结果,我的问题是为什么? This is nothing more then the example from the AngularJs webpage: ngClick example from AngularJs . 这只是AngularJs网页上的示例:AngularJs的ngClick示例

When I use that example without/outside <svg> (let say with button - like it is in official AngularJs webpage): 当我在没有<svg>没有<svg>情况下使用该示例时(请使用按钮说-就像在AngularJs官方网页中一样):

<button ng-click="count = count + 1" ng-init="count=0"> Click me! </button>

The AngularJs does his work. AngularJs从事他的工作。 Are there any restrictions regarding <svg> and AngularJs? 关于<svg>和AngularJs是否有任何限制? or perhaps I have to use some trick to use it? 还是我必须使用一些技巧才能使用它?

Great thanks in advance for any tip or help! 预先非常感谢您提供任何提示或帮助!

nykon 尼康

Angular directives will work just fine inside of SVG. Angular指令将在SVG内部正常工作。 See the below code, using your ng-click function: 请使用ng-click函数查看以下代码:

count: {{count}}
<svg width="100" height="100">
  <circle ng-click="count=count+1" ng-init="count=0" cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

SVG with angular ng-click example in plunker: http://plnkr.co/edit/KvHKa68oZ6YxQIJzyIXG?p=preview SVG中的plunker具有角ng-click示例: http ://plnkr.co/edit/KvHKa68oZ6YxQIJzyIXG?p=preview

Note that, to be accessed by javascript, loaded SVG files follow the same set of rules as other contents. 请注意,要通过javascript访问,已加载的SVG文件与其他内容遵循相同的规则集。 that means Same origin Policy may cause some troubles if you load your SVG from a different domain or a FILE source (that means your code MAY start working if you put it on a server like localhost) 这意味着如果您从其他域或FILE源加载SVG,则同源策略可能会引起一些麻烦(这意味着如果将其放在localhost之类的服务器上,代码可能会开始工作)

To access / reference SVG nodes you can use certain javascript libraries like Raphaël.js , although you can use straight java-script as well 要访问/引用SVG节点,您可以使用某些JavaScript库,例如Raphaël.js ,尽管您也可以使用直接的Java脚本

hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM