Ich versuche, ein Beispiel von LinkedIn zu folgen, aber mit den folgenden Problemen: 1. Ich versuche, localhost: Portname/Werte/GetStates aufzurufen, aber anstatt die Seite zu laden fordert es mich auf, die herunterzuladen JSON-Datei. 2. Als ich F12 verwendete, zeigte es mir nicht Angular und andere Referenzen, die ich meinem Projekt hinzufügte, kann nicht verstehen, warum es so ist? Ich habe versucht, mit Quellenangaben am oberen Rand der Seite wie folgt aus:Kann Angular Service nicht ausführen
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/Controllers/AngularController.js"></script>
<script src="~/Scripts/Services/AngularService.js"></script>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
und mit:
bundles.Add(new ScriptBundle("~/Scripts/angularscripts").Include(
"~/Scripts/angular.js",
"~/Scripts/AngularController.js",
"~/Scripts/AngularService.js"));
und dann einschließlich des Bündels Referenz wie folgt aus:
@section Scripts
{
@Scripts.Render("~/Scripts/angularscripts")
}
aber didn‘ t arbeiten entweder.
Mein Code ist einfach:
public class ValuesController : ApiController
{
[HttpGet]
public string GetStates()
{
List<States> states = new List<States>();
var state = new States();
state.Id = 1;
state.Name = "NSW";
states.Add(state);
state.Id = 2;
state.Name = "QLD";
states.Add(state);
state.Id = 3;
state.Name = "VIC";
states.Add(state);
state.Id = 4;
state.Name = "WA";
states.Add(state);
var result = JsonConvert.SerializeObject(states);
return result;
}
[HttpPost]
public string GetResult(States state)
{
var result = "State name is " + state.Name;
result = JsonConvert.SerializeObject(result);
return result;
}
}
und Angular-Code ist: AngularService.js
AngularModule.service('ApiCall', ['$http', function ($http) {
var result;
this.GetApiCall = function (controllerName, methodName) {
result = $http.get('api/' + controllerName + '/' + methodName).success(function (data, status) {
result = (data);
}).error(function() {
alert("Some problem in calling the service");
});
return result;
};
this.PostApiCall = function (controllerName, methodName, obj) {
$http.post('api/' + controllerName + '/' + methodName).success(function (data, status) {
result = (data);
}).error(function() {
alert("Some problem in calling the service");
});
return result;
};
}]);
AngularController.js
var AngularModule = angular.module('App', []);
AngularModule.controller('AppController', function ($scope, $http, ApiCall) {
$scope.message = "This is a test";
alert('App Controller');
var result = ApiCall.GetApiCall("Values", "GetStates").success(function (data) {
alert('data');
var result = $.parseJSON(JSON.parse(data));
$scope.StateList = result;
});
$scope.btnPostCall = function() {
var obj = {
'Name': 'NSW'
};
var result = ApiCall.PostApiCall("Values", "GetResult", obj).success(function (data) {
var result = $.parseJSON(JSON.parse(data));
$scope.message = result;
});
}
});
AngularService.js
AngularModule.service('ApiCall', ['$http', function ($http) {
var result;
this.GetApiCall = function (controllerName, methodName) {
result = $http.get('api/' + controllerName + '/' + methodName).success(function (data, status) {
result = (data);
}).error(function() {
alert("Some problem in calling the service");
});
return result;
};
this.PostApiCall = function (controllerName, methodName, obj) {
$http.post('api/' + controllerName + '/' + methodName).success(function (data, status) {
result = (data);
}).error(function() {
alert("Some problem in calling the service");
});
return result;
};
}]);
Ich führe dieses Projekt durch Anhängen von 'api/values / getstates' an localhost. Irgendwelche Ideen ???
}
}
Hallo sam, könnten Sie bitte Ihren Controller senden. Es scheint, dass Ihr Service-Code dupliziert ist, aber ich kann Ihren Controller nicht sehen. –
@LeoCaseiro Es ist aktualisiert, ich nehme an, dass mein Winkelcode überhaupt nicht aufgerufen wird, es scheint mir, dass nur die JSON-Daten des Web-Service zurückgegeben werden, bitte sehen Sie – sam