【发布时间】:2013-03-07 15:32:36
【问题描述】:
我想用嵌入在 HTML 页面中的 JSON 对象初始化 Angular 模型。示例:
<html>
<body>
<script type="text/javascript" charset="utf-8">
var tags = [{"name": "some json"}];
</script>
<ul>
<li ng-repeat="tag in tags">{{tag.name}}</li>
</ul>
</body>
</html>
无法解析tags 字段,因为它是在$scope 中查找的。我尝试像这样访问控制器中的tags 字段:
function TagList($scope, $rootScope) {
$scope.tags = $rootScope.tags;
}
但它不起作用。
仅当我将TagList 直接包含在 HTML 页面中并将 JSON 直接呈现到此函数中时,它才有效。
如何访问 Angular 控制器中单独 js 文件中的 tags 字段?
【问题讨论】:
标签: javascript json angularjs