- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何通过 JavaScript 将数据从 Angular Controller 传递到 Paper.js ?
这个问题与上一个问题几乎相同:
Pass Angular scope variable to Javascript
上一个问题的第一个答案对我来说不起作用(我在下面展示),并且我不清楚第二个答案(这也是一种解决方法)
edit: I figured out a bit cheesy solution that allows passing values from Angular to Javascript: I know it isn't the Angular way and likely has some other shortcomings (and I'd be more than happy to have them called out)-- I'm showing it in an answer below)
两年后现在有新的答案吗?
也许有 paper.js 直接方法? (之前的问题与 paper.js 无关)
我的代码和情况详细信息如下:
// snippet frome angular controller named "colorSelector"
$scope.xCoor = image.xCoor;
$window.xCoor = image.xCoor; //hoped this would work but doesn't
//angular works fine in the html
<p> {{description}}</p>
<p>{{xCoor}} is the X coordinate, {{yCoor}}</p>
编辑:删除了 ng-repeat 包装器,因为它并不直接应用于问题。(第二次编辑清理了更多内容)
//paper.js successfully has access to global JS variables, but
//I cannot get angular information into the JavaScript variables
<script type="text/javascript">
targetX = 250; targetY=270; //hardwired works (I'm only using x for example)
// targetX = {{image.xCoor}} doesn't work (no surprise)
// targetX = window.xCoor // I thought this might work? but it doesn't
// suggestion below from earlier S.O. question doesn't work for me
// targetX = angular.element(document.querySelector('[ng-controller="colorSelector"]')).scope().xCoor
</script>
<!-- paper.js section below -->
<script type="text/paperscript" canvas="myCanvas">
//below works if variables are set above
var target = new Path.Circle({
center: [targetX, targetY],
radius: 10,
strokeColor: "white"
});
</script>
如果上一个问题的第二个答案的“解决方法”方式仍然有效,有人可以为我充实一下吗? (他们建议插入一个带有脚本标签的字符串?)
最佳答案
我想出了一个简单的解决方法(不知道为什么花了这么长时间)我知道这不是 Angular 方式,并且可能还有一些其他缺点(我很乐意让他们指出)
// un-shown angular module defines $scope.xCoor
<div id="xCoor" class="hide-me-offscreen">
{{x-coor}}
</d>
<script type="text/javascript" >
var targetX = num(getElementById("x-coor").html)
</script>
关于javascript - 如何将数据从 Angular Controller 传递到 JavaScript 或 Paper.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33159456/
我是一名优秀的程序员,十分优秀!