Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple ng-change not working properly in angularjs

I might be missing something stupidly as this simple code doesn't work as expected. What is wrong is the $scope.change function in the MainCtrl doesn't work (no alert box popped up).

In a nutshell, the view is (it's in jade, better to view?)

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

and in the controller file

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');
    };

});

And the route:

$routeProvider
  .when('/', {
    templateUrl: 'views/main.html',
    controller: 'MainCtrl'
  });

Now what it is doing properly is that it correctly populates the view with the data created (i.e. the questions json). However, What it is not doing properly is the change() function bound to the input textbox doesn't work.

What am I missing here? This obviously is a very basic job.

like image 277
user3583721 Avatar asked May 27 '14 06:05

user3583721


People also ask

How do you change the value of an NG model?

If we use two way binding syntax for ngModel the value will be updated. So the default (ngModelChange) function will update the value of ngModel property. i.e., user.Name . And the second (ngModelChange) will be triggered printing the user name value in the console.

What is Ng change in AngularJS?

AngularJS ng-change Directive The ng-change directive tells AngularJS what to do when the value of an HTML element changes. The ng-change directive requires a ng-model directive to be present.

What can I use instead of NG-repeat?

The *ngFor directive in Angular is similar to the ng-repeat directive in AngularJS. It repeats the associated DOM element for each item in the specified collection.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.


2 Answers

ng-change requires ng-model as well.

<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>

Check out this JSFiddle.

like image 190
Sam P Avatar answered Sep 28 '22 07:09

Sam P


ng-change not working but it work if you define ng-model="yourValue"... like this

<select id="editSelect" ng-model="Option" ng-change="okChange()" name="Option">
    <option ng-repeat="data in Option" value="{{data.Option}}">{{data.Option}}</option>
</select>
like image 42
Pratik Parekh Avatar answered Sep 28 '22 06:09

Pratik Parekh