简体   繁体   English

带有JSON数据请求的角度ui-route

[英]Angular ui-route with JSON Data Request

I want to build a Single Page Website, where my data is stored in a CMS. 我想建立一个单页网站,我的数据存储在CMS中。 This CMS accepts Ajax Requests to serve me JSON. 该CMS接受Ajax请求为我提供JSON。 This JSON I want to output in my ng-app using the ui-router (I also tried the ngRoute before, with same results). 我想使用ui-routerng-app输出此JSON(我之前也尝试过ngRoute ,结果相同)。

The Problem is: I need no template. 问题是:我不需要模板。 Cause all my data I need comes from the JSON Request. 因为我需要的所有数据都来自JSON请求。 But using no template or templateUrl doesn't affects the controller. 但是不使用template或templateUrl不会影响控制器。

The question is how to output my received data in the HTML? 问题是如何在HTML中输出接收到的数据? I cant use ng-controller because it binds on only this specific controller. 我不能使用ng-controller因为它仅绑定在此特定控制器上。 Console.log shows that my data is successfully received, but I found no way to get an output. Console.log显示我的数据已成功接收,但是我找不到获取输出的方法。

app.config(function($stateProvider, $urlRouterProvider, $locationProvider) {

        $locationProvider.html5Mode(true);

        $urlRouterProvider.otherwise("/");

        $stateProvider
            .state('state1', {
                url: '/state1',
                template: '<h1>This Is A State</h1>',
                controller: function($scope, $http) {
                    $scope.pageObj = '';
                    $scope.pageObj.url = '/angular/demo/';
                    $scope.pageObj.class = 'page-my';
                    $scope.pageObj.data = 'Empty';
                    $http
                        .get('/angular/demo/')
                        .then(function(result) {
                            console.log("Data Received");
                            console.log(result.data);
                            $scope.pageObj.data = result.data;
                        });
                    //console.log(result.data);
                    console.log("Hello state");
                }
            });
    });

I found a small solution with using factory. 我发现使用工厂的小解决方案。 The question is still, how do I access my data? 问题仍然是,我该如何访问我的数据? Data now is stored by an own controller i guess. 我猜现在数据由自己的控制器存储了。 I post my updated code below: 我在下面发布更新后的代码:

 var app = angular.module('myApp', ['ngSanitize', 'ngRoute', 'ui.router']).factory("dataService", function($http) { var data = ""; var getData = function() { return data; } var setData = function(newData) { data = newData; } return { getData: getData, setData: setData }; }); app.controller('FactoryCtrl', ['$scope', '$http', 'dataService', function($scope, $http, dataService) { $scope.mydata = dataService.getData(); console.log($scope.mydata); }]); app.config(function($stateProvider, $urlRouterProvider, $locationProvider) { $locationProvider.html5Mode(true); $urlRouterProvider.otherwise("/"); $stateProvider .state('state1', { url: '/state1', template: '<h1>{{pageObj.data}}</h1>', controller: function($scope, $http, dataService) { $scope.pageObj = ''; $scope.pageObj.url = '/angular/demo/'; $scope.pageObj.class = 'page-my'; $scope.pageObj.data = 'Empty'; $http .get('/angular/demo/') .then(function(result) { console.log("Data Received"); $scope.pageObj.data = result.data; //$scope.model.data = dataService.getData(); dataService.setData("a string"); //console.log(dataService.getData()); }); console.log("Hello state"); } }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM