G
G
GodOfTheMetal2016-03-19 23:58:10
JavaScript
GodOfTheMetal, 2016-03-19 23:58:10

Why doesn't ng-checked work with append HTML code?

There is a code:

<body ng-app="App" ng-controller="AppCtrl">
<div class="container">
    <form ng-submit="newTask()">
        <input type="checkbox" ng-model="checkAll">
        <input id="one" type="text" ng-model="text" name="text" placeholder="What tododo?">
    </form>
    <input id="two" type="checkbox" ng-checked="checkAll">
</div>
</body>

var App = angular.module("App", []);

App.controller('AppCtrl', function ($scope) {
    $scope.list = [];
    $scope.newTask = function () {
        if ($scope.text) {
            $('.container').append("<div class='task'><input id="appended" type='checkbox' ng-checked='checkAll'/>" + this.text + "</div>");
            $scope.list.push(this.text);
            $scope.text = '';
        }
    };

});

Problem: the checkbox with id="two" reacts to changes in the checkbox with id="one", while the boxes added by the script with id="appended" do not. What is the problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IVAN SAVCHENKO, 2016-03-20
@gelevanog

Use ng-if="text" in HTML, don't go the jquery way

L
lega, 2016-03-20
@lega

They need to be compiled (bind), https://docs.angularjs.org/guide/compiler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question