【发布时间】:2015-05-22 03:36:05
【问题描述】:
我是 AngularJS 的新手,我正在尝试通过研究示例代码来理解它。
这里我有一个关于 $http.get 的内容,来自以下链接:
http://www.w3schools.com/angular/tryit.asp?filename=try_ng_customers_json
我刚刚用我自己的一个替换了 url 但它不起作用,我真的很困惑,请帮助,谢谢!
===========================
第二次编辑:感谢答案,双 ng-app 是一个错误,但这不是这个问题的主要原因。我认为这与跨站点阻止有关,但我已在我的 API 上将其关闭(我使用 codeigniter REST API 并设置了 $config['csrf_protection'] = FALSE; ),我不知道如何配置它。
<!DOCTYPE html>
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="customersCtrl">
<ul>
{{names}}
</ul>
</div>
<div ng-controller="myCtrl">
<ul>
{{names}}
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
$http.get("http://www.w3schools.com/website/Customers_JSON.php")
.success(function (response) {$scope.names = response;});
});
app.controller('myCtrl', function($scope, $http) {
$http.get("https://manage.pineconetassel.com/index.php/api/v1/colors2.php")
.success(function (response) {$scope.names = response;});
});
</script>
</body>
</html>
【问题讨论】:
-
提及文件扩展名,如:.php 或 .html 并尝试
-
第一个 ng-app 代码应该可以工作,其他将被忽略,将
ng-app放在 html 元素中会很好
标签: javascript angularjs api