简体   繁体   English

AngularJS ng-click 不起作用

[英]AngularJS ng-click is not working

I am trying to implement a small project in Angular JS and im stuck trying to call a function on ng-click which doesn't work however initial load is fine..what i want is to be able to recall the ajax with params which i have tested and works but can't call the function via the ng-click.我正在尝试在 Angular JS 中实现一个小项目,但我一直试图在 ng-click 上调用一个不起作用的函数,但是初始加载很好..我想要的是能够用我的参数调用 ajax已经测试并工作,但无法通过 ng-click 调用该函数。

i am using asp.net mvc5 and angularjs,我正在使用 asp.net mvc5 和 angularjs,

Index.chtml:索引.chtml:

@{
    ViewBag.Title = "Main";
}

<div class="wrapper wrapper-content" ng-app="mainModule" ng-controller="mainController">

    <div class="row">
        <div class="form-group col-lg-3 col-sm-6">
            <button type="submit" class="btn btn-warning" ng-click="getObject()">Apply</button>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-3 col-sm-6">
            <div>@Html.Partial("_Booking")</div>
        </div>
        <div class="col-lg-3 col-sm-6">
            <div>@Html.Partial("_Commission")</div>
        </div>
        <div class="col-lg-3 col-sm-6">
            <div>@Html.Partial("_Discount")</div>
        </div>
        <div class="col-lg-3 col-sm-6">
            <div>@Html.Partial("_Revenue")</div>
        </div>
    </div>

</div>

@section Scripts {

    @Scripts.Render("~/scripts/main")

}

main.js主文件

var mainModule = angular.module("mainModule", []);

mainModule.controller("mainController", [
       "$scope", "mainFactory", function ($scope, mainFactory) {
           $scope.returnedObject = {};
           mainFactory.getObject()
               .then(function (response) {
                   $scope.returnedObject = response.data;
                   console.log(response.data);
               }, function (error) {
                   console.error(error);
               });
       }
]);

mainModule.factory("mainFactory", function ($q, $http) {
    return {
        getObject: function () {
            var deferred = $q.defer(),
                httpPromise = $http({ method: "GET", url: "/Main/BookingData" });

            httpPromise.then(function (response) {
                deferred.resolve(response);
            }, function (error) {
                console.error(error);
            });

            return deferred.promise;
        }
    };
});

HTML should change to the following as it's not a form submission. HTML 应更改为以下内容,因为它不是表单提交。

<button type="button" class="btn btn-warning" ng-click="getObject()">

You need to bind getObject function in your controller.您需要在控制器中绑定getObject函数。 It should be:它应该是:

    $scope.getObject = function () {
              mainFactory.getObject()
                   .then(function (response) {
                       $scope.returnedObject = response.data;
                       console.log(response.data);
                   }, function (error) {
                       console.error(error);
                   });
    };


    // Run on load
    $scope.getObject();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM