与工厂保持数据的角度

时间:2017-08-17 12:56:38

标签: angularjs

我是棱角分明的新人,我有下一个问题。我有两个jsp,第一个在我想要在mi第二个视图上使用的变量中设置一个值,我想用工厂来做,mi的问题是第一个视图正确设置了值,但是当我想要恢复时它在第二个,它是未定义的。我已经研究过了,但我找不到答案,任何人都可以帮助我。

这是我的第一个观点。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3   /angular.min.js"></script>
<script src="Services.js"></script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body ng-app="Modulo" ng-controller="controladorForm">
<div class="form">

    <label for="nombre">AgregaNombre</label> 
    <input id="nombre" ng-model="nombre" type="text" />
    <button id="addBtn" class="btn" ng-click="add()">Añadir</button>
</div>
<div>{{nombreFac}}</div>
<a href="PresentaDatos.jsp">PresentaDatos</a>
</body>
</html>

services.js

var app = angular.module('Modulo', []);

app.factory('Factoria',function(){


var regreso = {};

return{ getSaludo : function(nombre) {
        regreso = nombre;
        alert(regreso);
        return regreso;
    },getData : function(){
        return regreso;
    }
    }
return regreso;

});



app.controller("controladorForm", ['$scope','Factoria', function($scope,Factoria){

$scope.add = function(){
    $scope.nombreFac = Factoria.getSaludo($scope.nombre);
}

$scope.get = function(){
     $scope.nombreFac = Factoria.getData();
}


}
]);


 app.controller("controladorPresenta", ['$scope','$rootScope','Factoria',     function($scope,$rootScope,Factoria){
 $scope.nombreFac = Factoria.getData();
 alert(Factoria.getData());
}
]);

mi第二视图

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script>
<script src="Services.js"></script>
</head>
<body  ng-app="Modulo" ng-controller="controladorPresenta">
<!-- <button id="addBtn" class="btn" ng-click="get()">Obtener</button> -->
{{nombreFac}}
</body>
</html>

0 个答案:

没有答案