Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用$stateparms动态加载JSON_Angularjs_Angular Ui - Fatal编程技术网

Angularjs 使用$stateparms动态加载JSON

Angularjs 使用$stateparms动态加载JSON,angularjs,angular-ui,Angularjs,Angular Ui,所以我要用Angular迈出第一步。在下面稍微简化的代码中,我尝试加载一个与URL匹配的JSON文件,例如ex1.html加载ex1.JSON var exerciseApp = angular.module('exerciseApp', ['ngAnimate', 'ui.router']); exerciseApp.config(function($stateProvider, $urlRouterProvider) { $urlRouterProvider.otherwise('/

所以我要用Angular迈出第一步。在下面稍微简化的代码中,我尝试加载一个与URL匹配的JSON文件,例如ex1.html加载ex1.JSON

var exerciseApp = angular.module('exerciseApp', ['ngAnimate', 'ui.router']);

exerciseApp.config(function($stateProvider, $urlRouterProvider) {
   $urlRouterProvider.otherwise('/');
   $stateProvider
    .state('exercise', {
      url: '/{exId:ex[0-9]{1,2}}',
      controller  : 'loadContent'
    });
});

exerciseApp.controller('loadContent', function($scope, $stateParams, $http) {
    $http.get('/content/'+$stateParams.exId+'.json').success(function(data){
      $scope.excontent=data;
    })
});
如果$http.get只显示“/content/ex1.json”,那么它就可以正常工作

实际上,应用程序似乎确实看到了ex.json文件。如果内容文件夹中有ex1.json文件,则不会返回“未找到文件”错误

我肯定我在其他地方看到过类似的东西。我做错了什么


解决:意识到我的错误。我没有在ui视图中包装要在视图模板中显示的值。JSON正在加载,但视图中没有显示它。

您是否使用fiddler查看get请求,或使用web浏览器的调试工具检查$stateParams.exId的值?您可能会在那里找到答案。您在浏览器栏中键入的实际值是多少?我在浏览器栏中键入的值类似于/ex1、/ex2。如果我在控制台中记录$stateParams.exID,我会看到“ex1”和“ex2”如我预期的那样出现。如果有一个匹配的JSON文件,就是它。如果没有,它会说找不到文件。更仔细地查看“获取”请求,它肯定是在查找和获取文件-问题似乎是它没有显示在视图中。已解决:意识到我的错误。我没有在模板中设置ui视图来显示内容。JSON正在加载,但视图中没有任何地方显示它。