Çava?
我正在开发一个应该能够从MVC .net服务器恢复数据并在angularjs前端可视化的程序。不幸的是没有用。这是代码:
MVC控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using MvcPrueba.Models;
using System.Diagnostics;
using System.Web.Mvc;
namespace MvcPrueba.Controllers
{
public class ValuesController : ApiController
{
#region camposPrivados
private static Logica l;
#endregion
#region metodosREST
// GET api/values
public IEnumerable<TablaEntrada> Get()
{
Debug.WriteLine("Llamada GetAll");
return l.getTEList();
//return new string[] { "value1", "value2" };
}
/*public JsonResult Get()
{
return l.getTEList();
}*/
// GET api/values/5
public string Get(int id)
{
return l.getSingleTE(id);
//return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
l.addNewTE(value);
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
l.modifyExistingTE(id, value);
}
// DELETE api/values/5
public void Delete(int id)
{
l.deleteExistingTE(id);
}
#endregion
#region metodosInicio
public static void App_Start()
{
l = Logica.fillListaTE();
}
#endregion
}
}
并且,angularjs工厂:
(function () {
var ETFactory = function ($http, $window) {
var TEs = [];
var factory = {};
factory.bidaliGetAllRequest = function () {
$http.get('http://localhost:50059/api/values').
success(function (response) {
TEs = response;
$window.alert("Http request OK, values:"+"\n"+TEs[0].descripcion+"\n"+TEs[1].descripcion+"\n"+TEs[2].descripcion);
}).error(function () {
$window.alert("error");
});
}
factory.getTEs = function () {
/*$http.get('http://localhost:50059/api/values').
success(function (response) {
TEs = response;
$window.alert("OK.\n"+movimientos[0].descripcion);
return TEs;
});*/
return http.get('http://localhost:50059/api/values');
}
factory.anadirNuevo = function (nuevo) {
}
/*
factory.cambiarvalor = function () {
if (a.valor == "a")
{
b.visible = false;
c.enabled = "checked";
movimientos[4].dominio = ["opcion1","opcion2","opcion3","opcion4","opcion5"];
}
else
{
b.visible = true;
c.enabled = "";
movimientos[4].dominio = ["opcion1","opcion2"];
}
};*/
return factory;
}
controlCajaApp.factory('ETFactory', ETFactory);
}());
我知道http请求到了后端,我做了一些尝试。事情是,在前端不会改变任何东西。有什么想法吗?
编辑:
开发人员工具网络标签和控制台的几个屏幕截图:
http://i.stack.imgur.com/W815y.jpg http://i.stack.imgur.com/usZ0f.jpg
EDIT2:
这是调用工厂的控制器:
(function () {
var ControlETCtrl = function (ETFactory, $scope, $http, $window) {
var scope = this;
scope.titulo = "Mi tabla de entradas";
scope.movimientos = ETFactory.getTEs();
//scope.movimientos = [];
scope.funciones = {};
scope.cargarDatos = function () {
/*ETFactory.bidaliGetAllRequest();*/
$http.get('http://localhost:50059/api/values').
success(function (response) {
this.movimientos = response;
$window.alert("OK.\n"+movimientos[0].descripcion);
});
}
function cargar (){
$http.get('http://localhost:50059/api/values').
success(function (response) {
this.movimientos = response;
$window.alert("OK.\n"+movimientos[0].descripcion);
});
};
scope.funciones.cambiarvalor = function () {
ETFactory.cambiarvalor();
}
scope.funciones.guardarMovimiento = function () {
/*
var auxCopyMov = angular.copy(scope.nuevoMovimiento);
auxCopyMov.tipo = scope.funciones.tipo(auxCopyMov);
ETFactory.postMovimiento(auxCopyMov);
scope.movimientos = ETFactory.getMovimientos();
scope.total = ETFactory.getTotal();
scope.nuevoMovimiento.importe = 0;
*/
}
}
controlCajaApp.controller('ControlETCtrl', ['ETFactory', ControlETCtrl]);
}());
最后是html视图,我在ng-repeat中使用来自控制器的变量movimientos:
<section name="ET" class="row-fluid">
<form class="form-horizontal text-left">
<fieldset>
<div id="legend">
<legend class="">{{controlET.titulo}}</legend>
</div>
<br>
<div>
<div class="control-group" ng-repeat="valor in controlET.movimientos" ng-show="valor.visible">
<label class="control-label">{{valor.descripcion}}</label>
<input ng-show="valor.tipo_visualizacion == 2" ng-disabled="valor.enabled" type="text" class="input" ng-model="valor.valor" ng-change="controlET.funciones.cambiarvalor()"></input>
<select ng-show="valor.tipo_visualizacion == 1" ng-disabled="valor.enabled" name="v" ng-model="valor.valor" ng-options="v for v in valor.dominio"></select>
<!--
<input type="checkbox" ng-show="valor.tipo_visualizacion == 3" ng-disabled="valor.enabled" type="text" class="input" ng-model="valor.valor" ng-change="controlET.funciones.cambiarvalor()">
-->
</div>
</div>
</fieldset>
</form>
</section>
答案 0 :(得分:1)
更改此
的服务方法factory.getTEs = function () {
var promise = $http.get('http://localhost:50059/api/values').
success(function (response) {
TEs = response;
$window.alert("OK.\n"+movimientos[0].descripcion);
return TEs;
});
return promise;
}
在你的控制器中写下这个来测试
ETFactory.getTEs().success(function(data){
scope.movimientos = data;
});