角度结构在某处不正确

时间:2015-06-16 19:46:29

标签: c# angularjs visual-studio-2012 angularjs-controller angularjs-controlleras

我要做的就是通过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;

        }

    }
} 

1 个答案:

答案 0 :(得分:3)

缺少以下内容

  1. ng-app指令必须位于body / html标记上,例如ng-app="app"
  2. 应在页面标题中加载脚本。
  3. 您必须在加载任何其他使用角度对象的文件
  4. 之前加载angular.js

    参考文献应

    <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;
          });
       } 
     }])