javascript标记中包含变量的AngularJS JSON数组
AngularJS JSON Array with variable in the javascript tag
我需要一个变量来标记JSON文件中的javascript。这是我的JSON文件的一部分
[
{
"title": "AfroEarth USA",
"url": "us",
"id": "50011",
"country" : "US",
"countryFlag": "img/flags/us-flag.png",
"countries": [
"AUSTRALIA",
"CANADA",
"SOUTH AFRICA",
"UNITED KINGDOM",
],
"countrieUrls": [
"/au",
"/can",
"/sa",
"/uk"
],
},
{
"title": "AfroEarth UNITED KINGDOM",
"url": "uk",
"id": "50011",
"country" : "UK",
"countryFlag": "img/flags/uk-flag.png",
"countries": [
"AUSTRALIA",
"CANADA",
"SOUTH AFRICA",
"UNITED STATES",
],
"countrieUrls": [
"/au",
"/can",
"/sa",
"/us"
],
},....
这是我的控制器的一部分
afroEarthApp.factory('Main', ['$resource', function ($resource) {
return $resource('js/afromain.json',{})
}])
afroEarthApp.controller('afroEarthMainCtrl',['$scope','$http','$location','Main', function($scope, $http, $location, Main) {
$scope.mainSite = Main.query();
}]);
还有我的html
<!doctype html>
<html lang="en" ng-app="afroEarthApp">
<head ng-controller="afroEarthMainCtrl">
<!-- Site Title -->
<title>{{mainSite[0].title}}</title>
</head>
<body class="home-page" ng-controller="afroEarthMainCtrl">
<ng-view></ng-view>
<script src="bower_components/angular/angular.js"></script>
<!--Angular.js Route-->
<script src="bower_components/angular-route/angular-route.js"></script>
<!--Angular.js Resource-->
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="js/controllers.js"></script>
<!-- WLD CAMPAIGN TRACKING -->
<script type="text/javascript">
$(document).ready(function(){
var $element = $('[ng-controller="afroEarthMainCtrl"]');
var scope = angular.element($element).scope().mainSite; // this is a problem
console.log(scope);
});
var wld_app_id = 312312;
var wld_app_url = "us.afroearth.com";
</script>
<title>{{mainSite[0].title}}</title>
-这是工作。var scope = angular.element($element).scope().mainSite;
-这是Array。但当我想得到第一个物体时。var scope = angular.element($element).scope().mainSite[0];
-这是不设防的。var scope = angular.element($element).scope().mainSite[0].id;
-也不起作用
我的阵列
您正试图在该信息可用之前获取该信息,如果该信息来自$resource,则意味着它只能在document.ready发生之后才可用。
此外,你绝对不应该在那里使用scope()
,如果你能更好地解释你正在做的事情,它会更容易提供帮助,如果你需要角度组件之外的全局变量中的一些资源值,你可以在那里声明变量,并在信息返回后为其赋值。
相关文章:
- AngularJS JSON not arriving php
- 将Excel/CSV文件中的数据导入angularjs-json对象中
- AngularJS json.parse 在 json 数据的第 1 行第 1 列处意外的数据结尾
- AngularJS JSON漏洞保护是如何工作的
- javascript标记中包含变量的AngularJS JSON数组
- AngularJS - Json to Tree structure
- AngularJS Json 数组的对象
- AngularJS JSON format
- AngularJS - jSON on date 顯示某些 ID
- Angularjs JSON treeview, json format issue
- Blueimp文件上传angularjs json前缀
- AngularJS Json Array
- 用额外的视图逻辑属性丰富angularjs-json数据
- 删除与另一个JSON javascript angularjs-JSON匹配的JSON
- AngularJS Json list
- AngularJS Json Editor
- 有没有办法为AngularJS JSON请求($http/$resource)使用浏览器缓存?
- 如何使用angularjs-json数据设置select 2下拉列表的默认值
- Angularjs json data
- AngularJS/JSON:通过值而不是索引来引用数组成员