1

我有 ASP.NET MVC 应用程序,我正在尝试简单的示例,其中提交表单由 ng-submit 处理,并使用 angularJS 输入警报值。我的 angularJS 的第一部分(在表 ng-repeat 中显示记录)正在工作,但没有形成

我想要的是 alert 叫

 vm.addTrip() = function () {
        alert(vm.newTrip.name); 
    };

当我按下表单上的提交按钮时

 <form novalidate name="NewTripForm" ng-submit="vm.addTrip()"></form>

我收到以下错误;(来自控制台....)

TypeError: vm.addTrip is not a function
at new tripsController (tripsController.js:31)
at Object.invoke (angular.js:4862)
at R.instance (angular.js:10717)
at n (angular.js:9594)
at g (angular.js:8903)
at g (angular.js:8906)
at g (angular.js:8906)
at angular.js:8768
at angular.js:1847
at m.$eval (angular.js:18017)

https://jsfiddle.net/toxic_kz/srs69ppp/2/

HTML

    <div>{{ "Two plus Two equals: " + (2+2) }}</div>

     <div ng-controller="tripsControllers as vm" class="col-md-6 col-md-offset-3" style="width:100%">
       <table class="table table-responsive table-striped">
           <tr ng-repeat="trip in vm.trips">
               <td>{{ trip.name }}</td>
               <td>{{ trip.created | date: 'dd-MM-yyyy'}}</td>
               <td><a href="#" class="btn btn-sm btn-primary">Manage</a></td>
           </tr>
       </table>

             <form novalidate name="NewTripForm" ng-submit="vm.addTrip()">
                 <div class="form-group">
                     <label for="name">New Trip Name</label>
                     <input class="form-control" type="text" id="name" name="name" ng-model="vm.newTrip.name" />
                 </div>

                 <div class="form-group">
                     <label>Testing Button</label>
                     <input class="btn btn-success" type="button" value="testing" id="testA" ng-click="alert('testing A Button')" />
                 </div>

                 <div class="form-group">
                     <input class="btn btn-success btn-sm" type="submit" value="Add" />
                 </div>
             </form>


    </div>

AngularJS

(function () {
"use strict";

angular.module("app-trips", []);
})();

(function () {
  "use strict";

angular.module("app-trips")
    .controller("tripsControllers", tripsController);

function tripsController()
{
    var vm = this;

    vm.trips = [{
        name: "US trip",
        created: new Date()
    },
    {
        name: "World trip",
        created: new Date()
    }
    ];

    vm.newTrip = {};

    vm.addTrip() = function () {
        alert(vm.newTrip.name); 
    };

  }

})();
4

1 回答 1

1

去掉多余的括号:

vm.addTrip() = function () {
        alert(vm.newTrip.name); 
    };

应该

vm.addTrip = function () {
        alert(vm.newTrip.name); 
    };
于 2017-03-11T18:28:21.687 回答