【问题标题】:use variable in both JQuery in an angularJS在 angularJS 中的两个 JQuery 中使用变量
【发布时间】:2016-03-31 05:03:16
【问题描述】:

我需要使用在 angularJS 控制器脚本内的 JQuery 脚本中定义的数组。我不明白 $rootScope 是如何融入整个事情的。

这可能吗,有什么建议吗?

$(document).ready(function(){ 
    $.get("KIP.xml",{},function(xml){

        // I need this accessible in controller.js
        FinalP = [ ];

    }
})

..

var app = angular.module("KIdash", []);

app.controller("controller", function($scope, $rootScope, $http) {

    console.log('initilaized!');

    $scope.PlayerList = **FinalP**
});

【问题讨论】:

  • 不要这样做,不要将变量从 jQuery 传递给 Angular。如果你想使用 $.get,为什么不使用 Angular 的 $http.get?
  • 我只使用 Angular 时遇到了很多麻烦... JQuery 样式甚至是第一个成功加载文件的样式。也许我应该回去再试一次。
  • 如果你想使用 Angular,我建议你阅读 Angular up & running,我相信这是最好的关于 Angular 的书籍之一。如果你刚刚开始使用 angular 并且不确定它是否是正确的选择,我建议你改用 ReactJS。

标签: jquery angularjs global-variables rootscope angularjs-rootscope


【解决方案1】:

您可以使用$window 对象https://docs.angularjs.org/api/ng/service/$window 传递变量

在 Jquery 中window.FinalP = []

在 Angular 中 $scope.SomeVar = $window.FinalP

【讨论】:

  • 非常感谢。它已经有点工作了。我可以在我的 Jquery 部分的 console.log(obj) 中看到该对象,但在角度来看,它在所有引用方式中都未定义。为了获得 $window 的资格,我需要做些什么吗? > console.log("类型化数组" + $window.PlayerStats); > console.log("从 FinalP "+ $window.PlayerStatW); > console.log("直接到 FinalP" +$window.FinalP);
  • app.controller("controller", function($scope, $rootScope, $http, $window) { console.log('initilaized!'); $scope.PlayerList = $window.FinalP; });
  • 即使访问一个简单的字符串或 int 也总是返回 undefined,但没有语法错误。嗯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多