【问题标题】:Ionic controllers and services离子控制器和服务
【发布时间】:2016-05-02 05:40:58
【问题描述】:

我对 Ionic 和 AngularJS 很陌生。我试图创建一个应用,但 app.js 的内容似乎是错误的。

这是我的代码:

app.js

angular.module('starter', ['ionic', 'starter.controllers'])
.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider.state('app', {
    url: "/ListeUsers",
    views: {
          templateUrl: "templates/ListeUsers.html",
          controller: 'UsersCtrl'
    }
});
  // if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/ListeUsers');
});

索引.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
	<script src="lib/ionic/js/angular/angular-resource.min.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/UserControllerIonic.js"></script>
	<script src="js/UsersServiceIonic.js"></script>
  </head>

  <body ng-app="starter">
    <ion-nav-view></ion-nav-view>
	
  </body>
</html>

userControllerIonic.js

angular.module('starter.controllers', ['starter.services'])



.controller('UsersCtrl', function($scope,userService) {
	
	
	$scope.Users=userService.getUsers();
	
});

UsersServiceIonic.js

angular.module('starter.services', ['ngResource'])


.factory('userService',function () {

		var Users =[];
        
        return{
			
			getUsers:function(){
				return $http.get("http://localhost:26309/api/User/getAll/").then(function (response) {
							users=response;
                            return users;
                        });
			},
			
			getUser : function(UserName) {
				return $http.get("http://localhost:26309/api/User/getUserByNom/" + UserName).then(function (serviceResp) {
					return serviceResp.data;
				});
			}
		}
		
})

ListeUsers.html

    <ion-list>
      <ion-item ng-repeat="user in Users"
               >{{user.nom}}</ion-item>
    </ion-list>
  

我找不到问题

【问题讨论】:

  • 你得到的确切错误是什么?
  • 什么都没有出现,一个空白页
  • 浏览器控制台说什么?那里应该有一个错误指向正确的方向
  • 没有错误,就像视图无法从控制器中检索数据
  • 您的“否则”路由 url $urlRouterProvider.otherwise('/app/ListeUsers'); 看起来不正确。请尝试这是否有效:$urlRouterProvider.otherwise('/ListeUsers');。然后它应该加载正确的状态。

标签: angularjs node.js ionic-framework


【解决方案1】:

您需要在此处解决 2 个问题才能让您的应用正常运行:

  1. 您需要将默认后备 URL 从 /app/ListeUsers 更正为 /ListeUsers,因为如果状态是 app 的子状态,/app/ListeUsers 路径将有效。在此处查看更多详细信息:https://github.com/angular-ui/ui-router/wiki/Nested-States-and-Nested-Views

  2. 您的服务方法正在返回承诺。 您不能将承诺分配给 $scope 变量并期望应用正常运行。更改您的控制器代码以使用返回的 Promise 的 .then 方法回调中的参数,如下所示:

    角度 .module('starter.controllers', ['starter.services']) .controller('UsersCtrl', function($scope,userService) {
    用户服务.getUsers() .then(函数(响应){ $scope.Users=响应; }); });

【讨论】:

  • 非常感谢您提供这个完整的答案,但它仍然不起作用,就像用户列表是空的,因为后端项目是在 ASP.NET 中使用 VS 实现的,并且正在实现前端带括号,所以我不知道这是否是我无法从数据库中检索数据的原因
  • 您能在开发工具的“网络”选项卡中看到响应吗?
  • 发生此错误:发生错误。 'ObjectContent`1' 类型未能序列化内容类型'application/xml 的响应正文;字符集=utf-8'。 System.InvalidOperationException ........等
猜你喜欢
  • 2017-09-28
  • 2019-04-13
  • 2015-08-18
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2018-01-31
相关资源
最近更新 更多