我要做的就是通过ng-Click和$ http.get来调用我的后端。然后我尝试设置我在html中显示的项目的值。这是我的结构。没有真正得到任何错误,所以任何有关如何查看我的代码不正确的信息都会很棒。感谢。
HTML
<head>
<title>Hello</title>
<script src="~/Scripts/profileController.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"> </script>
<section ng-controller="profileController as vm">
<div class="container">
<div style="padding-left:20px; padding-right: 1000px; height:700px;">
<h2>Create Profile</h2>
<img src="app/img/user.png" />
<label for="eventName">UserName: {{vm.newUser.UserName}}</label>
<input id="eventName" ng-model="UserName" type="text" placeholder="Edit User Name" />
<label for="eventName">Name: {{vm.nameName}}</label>
<input id="eventName" ng-model="nameName" type="text" placeholder="Edit Name" />
<label for="eventName">Age: {{vm.age}}</label>
<input id="eventName" ng-model="age" type="text" placeholder="Edit age" />
<label for="eventName">College: {{vm.college}}</label>
<input id="eventName" ng-model="college" type="text" placeholder="Edit college" />
<label for="eventName">City: {{data.city}}</label>
<input id="eventName" ng-model="name" type="text" placeholder="Edit city" />
<a class="btn btn-default btn-lg" ng-click="vm.getUserList()">Register</a>
</div>
</div>
</section>
控制器/ FACTORY
angular.module("app", [])
.controller('profileController', ['$scope', 'profileFactory', function ($scope, profileFactory) {
var vm = this;
this.getUserList = function () {
profileFactory.getUserList($scope).success(function (data) {
$scope.UserList = data;
});
}
}])
.factory('profileFactory', ['$http', '$q', function ($http, $q) {
var urlBase = "/api/saveUser";
var dealerProcessReportSetupFactory = {
getUserList: function () {
return $http.get(urlBase + "/UserList");
}
};
return dealerProcessReportSetupFactory;
}]);
API CONTROLLER
public class profileController : ApiController
{
// GET api/DealerProcessReportSetup
[HttpGet]
[ActionName("saveUser")]
public tblOwl getUserList()
{
using (VSCDevEntities db = new VSCDevEntities())
{
var Owl = (from z in db.tblOwls select z).FirstOrDefault();
return Owl;
}
}
}
答案 0 :(得分:3)
缺少以下内容
ng-app
指令必须位于body
/ html
标记上,例如ng-app="app"
。参考文献应
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<script src="~/Scripts/profileController.js"></script>
<强>更新强>
当您使用controllerAs
语法时,您的控制器应使用this
关键字,或至少您应该使用此关键字定义getUserList()
方法ng-click="vm.getUserList()"
。
<强>控制器强>
angular.module("app", [])
.controller('profileController', ['$scope', 'profileFactory', function ($scope, profileFactory) {
var vm = this;
this.getUserList = function () {
profileFactory.getUserList($scope).success(function (data) {
$scope.UserList = data;
});
}
}])