简单的ng-change在angularjs中无法正常工作

use*_*721 10 javascript angularjs

我可能会丢失一些愚蠢的东西,因为这个简单的代码不能按预期工作.有什么问题是$scope.changeMainCtrl中的功能不起作用(没有弹出警告框).

简而言之,视图是(它是在玉中,更好地查看?)

<body ng-app="epfApp">
...
label(ng-repeat="question in questions")
    | {{ question.title }}
    input(type="{{question.type}}", ng-change="change()")
Run Code Online (Sandbox Code Playgroud)

并在控制器文件中

angular.module('epfApp')
.controller('MainCtrl', function ($scope, $window) {

    $scope.questions = {
        '1': {
                'title': 'The first question is?',
                'type': 'text',
                'placeholder': 'first question'
            },

        '2': {
                'title': 'The second question is?',
                'type': 'text',
                'placeholder': 'second question'
            }
    };

    $scope.change = function() {
        $window.alert('text');
    };

});
Run Code Online (Sandbox Code Playgroud)

路线:

$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl'
  });
Run Code Online (Sandbox Code Playgroud)

现在正在做的是它正确地用创建的数据填充视图(即问题json).但是,它没有正常运行的是change()绑定到输入文本框的功能不起作用.

我在这里错过了什么?这显然是一项非常基本的工作.

Sam*_*m P 23

ng-change也需要ng-model.

<div ng-controller="MainCtrl">
    <label ng-repeat="question in questions">
        {{question.title}}
        <input type="{{question.type}}" ng-model="question.placeholder" ng-change="change()" />
        <br />
    </label>
</div>
Run Code Online (Sandbox Code Playgroud)

看看这个JSFiddle.


Pra*_*ekh 4

ng-change 不起作用,但如果你定义 ng-model="yourValue"... 就像这样

<select id="editSelect" ng-model="Option" ng-change="okChange()" name="Option">
    <option ng-repeat="data in Option" value="{{data.Option}}">{{data.Option}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)