无法将解析对象从ui-route更改注入控制器

时间:2018-03-06 09:08:53

标签: angularjs angular-ui-router

我正在尝试使用ui route访问路由更改期间调用的http get响应。解决方案本身发生但我无法访问响应。我已经尝试了“listcontroller”中的一些方法。我正在使用在互联网上找到的示例httpget url进行测试。角度版本:v1.2.32。 ui-route:1.0.15。

的script.js

var app = angular.module("Rasp", ["ui.router"])
.config(['$stateProvider', '$urlRouterProvider',function ($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise("/home");
    $stateProvider
        .state("ipList", {
            url: "/ipList",
            templateUrl: "templates/list.html",
            controller: "listController",
            resolve: {
                SensorIP : function($http){
                    $http.get('https://httpbin.org/ip')
                    .then(function(response){
                        console.log('res');
                        console.log(response);
                        return response.data.origin;
                    });
                }
            }
        })
        .state("home", {
            url: "/home",
            templateUrl: "templates/home.html",
            controller: "homeController"
        });
}])

.controller("RaspController", ['$scope', '$http', function ($scope, $http) {
    $scope.getDetails = function () {
//            $http.get('https://httpbin.org/ip')
//                .then(function (response) {
//                    console.log(response);
//                    $scope.response = response;
//                },
//                function (error) { console.log(error); }
//                );
//        };

}])

.controller("homeController", ['$scope', function ($scope) {


}])
.controller("listController", ['$scope','SensorIP',function ($scope,SensorIP) {
    $scope.sensorList = SensorIP;
    var vm = this;
    this.list1 = SensorIP;
    var logfunc = function() {console.log($scope.sensorlist)};

}])

list.html

<div>
 {{list1}}
 {{sensorlist}}
 <button class="btn btn-danger navbar-btn" ng-click="logfunc()">click</button>
 </div>

home.html的

<h4>Click on "Get IP List" to get the list of IPs</h4>
<a ui-sref="ipList"><button id="button" >Get IP List</button></a>

的index.html

    <!DOCTYPE html>
<head>
    <title>Title(To be changed)</title>
    <!--JS-->
    <script type="text/javascript" src="../node_modules/angular/angular.js"></script>
    <script type="text/javascript" src="../node_modules/@uirouter/angularjs/release/angular-ui-router.js"></script>
    <script type="text/javascript" src="script.js"></script>
    <!-- -->
    <!--CSS-->
    <link rel="stylesheet" href="../css/style.css">
    <!-- -->
</head>
<body>
    <div ng-app="Rasp">
        <div ng-controller="RaspController">
            <div class="sidenav">
<!--
                <a ui-sref="home" id="dot-button"><button class="btn btn-danger navbar-btn" >Home</button></a>
                <a ui-sref="ipList" id="dot-button"><button class="btn btn-danger navbar-btn" >IP List</button></a>
-->
                <a ui-sref="home">home</a>
                <a ui-sref="ipList" >ipList</a>
            </div>
            <div>
                <ui-view id="uiview"></ui-view>
            </div>
        </div>
    </div>
</body>

0 个答案:

没有答案