【问题标题】:send value to controller in angularjs and return array在angularjs中向控制器发送值并返回数组
【发布时间】:2018-05-08 16:32:18
【问题描述】:

我想将 cod 发送到控制器并返回数组

按下li 时,您必须向控制器发送代码,在这种情况下,我设置了示例 7。代码到达控制器后,我将有一个列表,我必须在 ng 中显示- 在表中重复

脚本

<script type="text/javascript">

    var app = angular.module('myApp', [])

    app.value('studentInfo', [
      { id: 1, name: 'Mahedee Hasan', credit: 20, semester: '8th' },
      { id: 3, name: 'Enamul Haque', credit: 15, semester: '7th' }
    ]);

    app.controller('myCtrl', ['$scope', 'studentInfo', function ($scope, studentInfo, $http, $window) {
        $scope.myClickList = function () {
            $scope.studentInfo = studentInfo;
        };

        var Cod = "7";
        $scope.myDataCountry = [];
        $scope.ButtonCountry = function (Cod) {
            $http.
                post("/Country/Angular", { CodH: Cod }).success(function (result) {
                $scope.myDataCountry = result;
            });
        };

    }]
  );
</script>

查看

<li><a data-toggle="tab" href="#calificaciones" ng-click="ButtonCountry ()"><span>Country</span></a></li>


    <div ng-app="myApp" ng-controller="myCtrl">
<table class="table">
    <tr>
        <th>ID</th>
        <th>Country</th>
    </tr>
    <tr ng-repeat="C in myDataCountry">
        <td>{{C.ID}}</td>
        <td>{{C.Country}}</td>
    </tr>
</table>
    </div>

控制器

    public JsonResult Angular(string codCountry)
    {

        var country = (from a in dbCountry.Country
                         where a.CodPersona == codCountry
                         select a.Country).ToList();

        return Json(country , JsonRequestBehavior.AllowGet);
    }

I tried thisto

【问题讨论】:

  • ng-click 位于控制器范围之外的元素上。控制器的内联数组注解与依赖注入不匹配。
  • .success 方法是deprecated and removed from V1.6
  • 您的 MVC 控制器的名称是什么?是 CountryController 吗?

标签: asp.net angularjs asp.net-mvc angularjs-ng-repeat angularjs-ng-click


【解决方案1】:

首先,你的 li 元素不在你的 app 指令中,这意味着它不会检测到函数,你需要确保你的 li 元素在 app 范围内

<!-- li is outside the scope --> 
<li><a data-toggle="tab" href="#calificaciones" ng-click="ButtonCountry(1)"><span>Country</span></a></li>
    <div ng-app="myApp" ng-controller="myCtrl">
<!-- end --> 

<!-- li is within the scope --> 

    <div ng-app="myApp" ng-controller="myCtrl">
<ul>
<li><a data-toggle="tab" href="#calificaciones" ng-click="ButtonCountry(1)"><span>Country</span></a></li></ul> 
<!-- end --> 

当然,您需要更改 html 元素,这意味着 li 的父级 ul 大多数也被包含在内。

您的操作 url 错误,您的控制器显示操作名称为 CalificacionesAngular,但由于某种原因您正在使用 Angular,另一件事我注意到您从未将代码传递给您的函数,这意味着 这个

ng-click="ButtonCountry ()" 
 //should be this 
ng-click="ButtonCountry('thecode')" 

并且您发布的数据与参数名称不相似, 你必须改变这个

post("/Country/Angular", { CodH: Cod })
//to this 
post("/Country/CalificacionesAngular", { codCountry: Cod })

可能还有一些问题,这些是我目前看到的问题,请调试并提供有关您遇到的错误的更多详细信息。 thisthis 也是一个很好的例子,我建议阅读有关 DirectivesBindingscopeEvents 的信息

【讨论】:

  • ng-click 位于控制器范围之外的元素上。控制器的内联数组注解与依赖注入不匹配。
  • 谢谢乔治,我刚刚注意到,我会编辑我的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
相关资源
最近更新 更多