【问题标题】:Can't fetch single record in Angular js/HTML无法在 Angular js/HTML 中获取单个记录
【发布时间】:2015-05-26 19:54:38
【问题描述】:

希望你做得好..

我正在尝试通过Angular-js 在 UI 中按 ID 从数据源中获取单个记录。 使用 Web-API 从 DB 中检索值。

为了简单起见:HTML-->Angular-->WebAPI-->DB

当我尝试时,它说通过的 ID 为 Null..不知道如何纠正。 希望我错过了填补某个地方的漏洞....在 sn-ps fr ref 下方。

(你也可以验证/纠正我在 html 中编码的方式是显示 Id 获取的值的正确方式)

HTML:

<div ng-controller="SingleController">
        <input type="text" ng-model="_Id" />
        <input type="button" value="search" ng-click="search()" />
        <table>
            <tr>
                <td>MovieId</td>
                <td>{{MovID}}</td>
            </tr>
            <tr>
                <td>Title</td>
                <td>{{Movtitle}}</td>
            </tr>
            <tr>
                <td>Genre</td>
                <td>{{Movgnre}}</td>
            </tr>
            <tr>
                <td>Classification</td>
                <td>{{Movcls}}</td>
            </tr>
            <tr>
                <td>ReleaseDate</td>
                <td>{{Movdate}}</td>
            </tr>
            <tr>
                <td>Rating</td>
                <td>{{Movrate}}</td>
            </tr>
            <tr>
                <td>Cast</td>
                <td>{{Cast}}</td>
            </tr>
        </table>
    </div>

控制器.JS

app.controller('SingleController', function ($scope, MyService) {
var Id = $scope._Id;
$scope.search = function (Id) {
    var promiseGetSingle = MyService.getbyId(Id);

    promiseGetSingle.then(function (pl) {
        var res = pl.data;
        $scope.MovID = res._movieId;
        $scope.Movtitle = res._title;
        $scope.Movgnre = res._genre;
        $scope.Movcls = res._classification;
        $scope.Movdate = res._releaseDate;
        $scope.Movrate = res._rating;
        $scope.Cast = res._cast;

        //   $scope.IsNewRecord = 0;
    },
              function (errorPl) {
                  console.log('failure loading Employee', errorPl);
              });
    }
});

service.js

this.getbyId = function (Id) {
    return $http.get("/api/values/" + Id);
};

请忽略这个冗长的 sn-ps。 你能帮我解决这个问题吗?

【问题讨论】:

  • 您确定 $http.get() 函数中的相对 URL 吗?
  • @dustmouse 是的.....它的正确路径

标签: javascript html angularjs asp.net-web-api


【解决方案1】:

您的搜索功能希望您在 ng-click 上调用它时传递一个值:

<input type="button" value="search" ng-click="search(_Id)" />

【讨论】:

  • 这就是答案...非常感谢兄弟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 2018-09-07
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多