0

http://plnkr.co/edit/0W4ATi7loAKk94Cnj3lz?p=preview

一个简单的 Angular 应用程序,不知道为什么ng-submit没有触发该createMeetup功能

HTML

<!-- Meetups View -->
<div ng-controller="meetupsController">
    <h1>There are {{meetupsCount}} meetups</h1> 
    <ul>
        <li ng-repeat="meetup in meetups">
            {{meetup.name}}
        </li>
    </ul>
</div>

<form ng-submit="createMeetup()">
    <input ng-model="meetupName" type="text" placeholder="Meetup name"></input>
    <button type="submit">Add</button>
</form>

控制器

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

.controller('meetupsController', ['$scope', function($scope) {
    $scope.meetupsCount = 10;

    $scope.meetups = [
        { name: "MEAN SF Developers" },
        { name: "Some other meetup" },
    ]

    $scope.createMeetup = function() {
        console.log('clicked');
        $scope.meetups.push({ name: $scope.meetupName });
        $scope.meetupName = '';
    }
}]);
4

1 回答 1

3

表单元素位于控制器范围之外。

<!-- Meetups View -->
<div ng-controller="meetupsController">
    <h1>There are {{meetupsCount}} meetups</h1> 
    <ul>
        <li ng-repeat="meetup in meetups">
            {{meetup.name}}
        </li>
    </ul>
    <form ng-submit="createMeetup()">
           <input ng-model="meetupName" type="text" placeholder="Meetup name"></input>
          <button type="submit">Add</button>
    </form>
</div>
于 2015-02-18T17:20:58.017 回答