为什么ng-repeat中没有显示任何内容?

时间:2017-04-15 11:28:29

标签: html angularjs json

现在我有一个返回arraylist的spring服务..我做了一个ifrt来测试在控制器中返回的数组并且它成功地作为JSON对象出现..但是在Html文件中的ng重复中没有显示

HTML文件:



<!DOCTYPE html>
<html ng-app="phase2">

<head>
<title>Student Page</title>
<script>document.write('<base href="' + document.location + '" />');</script>
    <link href="style.css" rel="stylesheet" />
    <script data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js" data-require="angular.js@1.2.x"></script>
    <script src="https://code.angularjs.org/1.2.28/angular-route.js"></script>
	<script src="LogInController.js"></script>
</head>

<body>
<div ng-app="phase2" >
	<div ng-controller="load" >
		<tr ng-repeat="x in sharedData">
			<td> {{x.name}} </td> 
			<td> => {{x.score}} </td>
		</tr>
		
	</div>
</div>
</body>
</html>
&#13;
&#13;
&#13;

LogInController.js:

&#13;
&#13;
var app = angular.module("phase2" , ['ngRoute'])

app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider){
    $routeProvider
		.when("http://localhost:8060/TheAngular_Project/LogInPage.html", {
			templateUrl: "LogInPage.html",
			controller: "LogIn"
		})
		.when("http://localhost:8060/TheAngular_Project/StudentPage.html", {
			templateUrl: "StudentPage.html",
			controller: "load"
		})
		.when("http://localhost:8060/TheAngular_Project/TeacherPage.html", {
			templateUrl: "TeacherPage.html",
			controller: "load"
		})
		// .otherwise({ redirectTo: '/'})
		;
}]);

app.controller( "LogIn" ,function ($scope , $http , srvShareData , $location)
		{
			$scope.dataToShare = [];
			$scope.save = function() {
				var email= document.getElementById("email").value;
				var Pass=document.getElementById("Pass").value;
				var Info ;
				$http.get('http://localhost:8090/LogIn/'+email+'/'+Pass)
				.then(function(response)
					{
						Info = JSON.stringify(response.data);
						$scope.dataToShare = Info ;
						srvShareData.addData($scope.dataToShare);
						//$scope.day=srvShareData.getData();
						if ($scope.dataToShare.schema)
							{
							window.location.href="http://localhost:8060/TheAngular_Project/TeacherPage.html";
							}
						else
							{
							window.location.href="http://localhost:8060/TheAngular_Project/StudentPage.html";
							}
					});
				}
		});

app.controller("load" , function($scope,  srvShareData )
	{
	
			$scope.sharedData = srvShareData.getData();
			alert($scope.sharedData)
	});


app.service('srvShareData', function($window) {
    var KEY = 'App.SelectedValue';

    var addData = function(newObj) {
    	//$window.sessionStorage.clear();
        var mydata = $window.sessionStorage.getItem(KEY);
        if (mydata) {
            mydata = JSON.parse(mydata);
        } else {
            mydata = [];
        }
        mydata = newObj;
       
        $window.sessionStorage.setItem(KEY, JSON.stringify(mydata));
    };

    var getData = function(){
        var mydata = $window.sessionStorage.getItem(KEY);
        if (mydata) {
            mydata = JSON.parse(mydata);
        }
        
        return mydata || [];
    };

    return {
        addData: addData,
        getData: getData
    };
});
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

html中的一些更改及其正常工作(您也不必再次使用ng-app):

<强> HTML:

<body ng-controller="mainCtrl" class="container" style="padding-top:30px">
    <table>
      <tr ng-repeat="x in sharedData">
        <td> {{x.name}} </td>
        <td> => {{x.score}} </td>
      </tr>
    </table>
  </body>

<强>脚本:

angular.module('app', []);

angular.module('app').controller('mainCtrl', function($scope) {
  $scope.sharedData = [{
    name: 'Subtraction',
    score: 1500
  }]

});

这是工作的Plnkr:http://plnkr.co/edit/I1mfsgJfgsw1b92lJCkO?p=preview

相关问题