3

So I'm new to Angularjs, but I'm trying to write the code for a comment area where users can put their comments. For this I'm using a div element with conteneditable="true" property. Here's my code for html:

<html>
    <script src="./includes/angular.min.js"></script>
    <script src="./includes/angular.route.js"></script>
    <script src="./includes/angular.sanitize.js"></script>
    <script src="./includes/test.js"></script>  
    <body ng-app="testapp" ng-controller="cnt1"> 

        <div contenteditable="true" ng-model='comment'>{{comment}} </div>
        <input ng-model='comment' type='text'/><br>

        <div>{{comment}}</div>    
    </body>    
</html>

and here is the .js code

/// <reference path="./angular.min.js"
/// <reference path="./angular.route.js"
/// <reference path="./angular.sanitize.js"
/// <reference path="./jq/jquery-2.0.3.min.js"
/// <reference path="./moment.min.js"
/// <reference path="https://ajax.googleapis.com/ajax/libs/webfont/1.4.7/webfont.js"

var app = angular.module("testapp", ['ngRoute', 'ngSanitize']);
app.controller("cnt1", function ($scope, $http, $rootScope, $timeout) {
    $scope.comment = "type here";

});

I don't understand why ng-model updates the "comment" variable in the input element but it does not update the variable for the div element! Any help is appreciated! And if $apply() can fix this, how should I use it so it updates the "comment" variable in the div element?

1 Answer 1

5

https://docs.angularjs.org/api/ng/directive/ngModel

The ngModel directive binds an input, select, textarea (or custom form control) to a property on the scope using NgModelController, which is created and exposed by this directive.

divs are not listed there. :(

If you're still looking to work w/ a div, take a look at the example here! https://docs.angularjs.org/api/ng/type/ngModel.NgModelController#custom-control-example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.