简体   繁体   English

在angularJS的控制器中绑定数据

[英]To bind the data in the controller of the angularJS

I have dynamically created the div element in the controller function of my angularjs, so in the div element want to bind the data but it is not binding it is showing the text as it is.我已经在我的 angularjs 的控制器函数中动态创建了 div 元素,所以在 div 元素中想要绑定数据但它没有绑定它显示文本原样。

My code is:我的代码是:

function botCtrl($scope,$rootScope,$state,$window,$http)
{
    $scope.messageContent = '';

    //console.log("ID ::", $state.params.chat_state_id );
    $scope.submit = function(){
        console.log("Message ::", $scope.message);

        $scope.messageToShow = $scope.message;
        var newEle = angular.element("  <div class='message' ><a class='message-author' ng-model='author'> You </a>\
            <span class='message-content' ng-model='messageContent'>{{messageToShow}}\
        </span> </div>");
        var target = document.getElementById('messageDiv');
        angular.element(target).append(newEle);

        var data = {
            id: "775525ad-3c95-4ff4-aac7-5260466fc146",
            message: $scope.message
        }
            $scope.messageContent = $scope.message;
            $scope.value = 1;
            var url = "/api/chatBot/chatBot";
            $http.post(url,data)
                .success(function(data){
                    $scope.messageToShow = data.messageText;
                    console.log("Message ::", $scope.messageToShow);
                    var newEle = angular.element("<div class='message' ><a class='message-author' ng-model='author'> Bot </a>\
                    <span class='message-content' ng-model='messageContent'>{{messagetoShow}}\
                </span> </div>");
                var target = document.getElementById('messageDiv');
                angular.element(target).append(newEle);
                }) .error(function(){
                    console.log("ERROR in bot controller");
                })

    }


}

It is creating the div element in both the cases, but the data inside div of both the cases is coming {{messageToShow}} it is not binding the data.它在两种情况下都创建了 div 元素,但是两种情况下 div 内的数据都来了 {{messageToShow}} 它没有绑定数据。

So where I am doing wrong.所以我哪里做错了。

Thanks谢谢

I found a solution.我找到了解决方案。

Code:代码:

function botCtrl($scope,$rootScope,$state,$window,$http) {
    $scope.messageContent = '';

    //console.log("ID ::", $state.params.chat_state_id );
    $scope.submit = function() {
        console.log("Message ::", $scope.message);

        $scope.messageToShow = $scope.message;
        var newEle = angular.element("  <div class='message' ><a class='message-author' ng-model='author'> You </a>\
            <span class='message-content' ng-model='messageContent'>"+$scope.messageToShow+"\
        </span> </div>");
        var target = document.getElementById('messageDiv');
        angular.element(target).append(newEle);

        var data = {
            id: "775525ad-3c95-4ff4-aac7-5260466fc146",
            message: $scope.message
        }
            $scope.messageContent = $scope.message;
            $scope.value = 1;
            var url = "/api/chatBot/chatBot";
            $http.post(url,data)
                .success(function(data) {
                    $scope.messageToShow = data.messageText;
                    console.log("Message ::", $scope.messageToShow);
                    var newEle = angular.element("<div class='message' ><a class='message-author' ng-model='author'> Bot </a>\
                    <span class='message-content' ng-model='messageContent'>"+$scope.messageToShow+"\
                </span> </div>");
                var target = document.getElementById('messageDiv');
                angular.element(target).append(newEle);
                $scope.message = "";
                }) .error(function(){
                    console.log("ERROR in bot controller");
                })                
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM