0

I'm stuck with this for the whole day.

<table>
    <tr ng-repeat="x in usernames">
        <td>{{x.username}}</td>
        <td>
            <button type='button' ng-click='sendMessage()'>Send  Message</button>
        </td>
        <td>
            <button type="button" ng-click="deleteUser()">Delete User</button>
        </td>
    </tr>
</table>

When I press "Send Message", controller is ignited successfully.

$scope.sendMessage = function () {
    $scope.content_in = '<form novalidate>'
        +'<label>Message: &nbsp</label> <input type="text" ng-model="message"/><br><br>'
        +'<button id="mybutton" style="width:75px" ng-click="send()">Send</button>'
        +'<input id="mycancel" style="width:75px" type="reset" value="Cancel"/>'
        +'</form>';
}

I'm simply trying to update my page with a message form which will be placed in content_in variable. But the changes are not reflected in the view.

I also included:

$scope.$watch('content_in', function () {
    alert('hey, content_in has changed!');
});

in the controller, and I'm confident that the changes are made in controller. How can I make the view reflect the changes?

4
  • 1
    I haven't noticed any usage of content_in in your HTML. Please provide some relevant markup or, it would be the best, a jsFiddle or plunker. Thx! Commented Jul 15, 2016 at 11:06
  • Try to use $scope.$apply Commented Jul 15, 2016 at 11:10
  • 1
    Couldn't you use ngShow ngIf or ngBindHtml instead? Commented Jul 15, 2016 at 11:23
  • $scope.$apply - not recommended to be used to force apply.... Please provide more code to see the problem. Commented Jul 15, 2016 at 12:24

1 Answer 1

1

Try the following approach

 myApp.controller("myController", function ($scope,  $compile, myService) 
{
    $scope.sendMessage = function () {
        var content_in = '<form novalidate>'
        +'<label>Message: &nbsp</label> <input type="text" ng-model="message" /><br><br>'
        +'<button id="mybutton" style="width:75px" ng-click="send()">Send</button>'
        +'<input id="mycancel" style="width:75px" type="reset" value="Cancel" />'
        +'</form>';
        var element = angular.element(document.querySelector("#myDiv"));
        element.empty();
        element.append(content_in);
        $compile(element.contents())($scope);
    }
});

In your design page add this div

<div id="myDiv">

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.