简体   繁体   English

使用Angular JS将HTML附加到div

[英]Appending HTML to a div using Angular JS

I'm currently building an Umbraco dashboard extension using AngularJS and was wondering if there was a means by which I can append HTML to a div on my page. 我目前正在使用AngularJS构建Umbraco仪表板扩展,并且想知道是否有一种方法可以将HTML附加到我页面上的div。

The idea is that I want to create a sort of history pane that is updated every time a user clicks a button to trigger a web request to our web services. 我的想法是,我想创建一种历史窗格,每次用户单击按钮以触发对Web服务的Web请求时,该窗格都会更新。 The web request then returns each of the pages that have been updated in Umbraco as well as a link to each page. 然后,Web请求返回Umbraco中已更新的每个页面以及每个页面的链接。

So far I have the following: 到目前为止,我有以下内容:

HTML HTML

<div ng-controller="AxumTailorMade" class="container-fluid">
    <div class="row">
        <div class="col-md-12 heading clearfix">
            <h3>Axum Integration</h3>
            <img class="pull-right" src="/App_Plugins/Axum/css/images/logo.png" />
        </div>
    </div>
    <div class="row">
        <div class="info-window" ng-bind-html="info">

        </div>
        <div class="col-md-3 update-type">
            <h4>Update All Content</h4>
            <p>Synchronise all content changes that have occured in the past 24 hours.</p>
            <span><button class="button button-axum" type="button" ng-disabled="loadAll" ng-click="getAll()">Update</button><img src="/App_Plugins/Axum/css/images/loader.gif" ng-show="loadAll" /></span>
        </div>
   </div>
</div>

My angular controller is as so: 我的角度控制器是这样的:

angular.module("umbraco")
    .controller("AxumTailorMade",
    function ($scope, $http, AxumTailorMade, notificationsService) {
        $scope.getAll = function() {
            $scope.loadAll = true;
            $scope.info = "Retreiving updates";
            AxumTailorMade.getAll().success(function (data) {
                if (!data.Result) {
                    $scope.info = null;
                    notificationsService.error("Error", data.Message);
                } else if (data.Result) {
                    $scope.info = "Content updated";
                    notificationsService.success("Success", data.Message);
                }
                $scope.loadAll = false;
            });
        };
    });

I assumed that like jQuery there would be some form of named append function but that doesn't look like that is the case so previously I tried: 我假设像jQuery一样会有某种形式的命名追加函数,但看起来不是这样的,所以我以前尝试过:

$scope.info = $scope.info + "content updated";

But this would return 但这会回来

undefinedcontent updated

So my question is how do I output returned HTML to the info div without deleting the content that is already in there (if there is any). 所以我的问题是如何将返回的HTML输出到info div而不删除已经存在的内容(如果有的话)。

Any help would be greatly appreciated as this is my first real attempt with Angular. 任何帮助将不胜感激,因为这是我第一次使用Angular的真正尝试。

I think the problem with your earlier attempt is that the $scope.info was undefined the first time you tried to append to it. 我认为您之前尝试的问题是$ scope.info在您第一次尝试追加时未定义。 If it had been initialized with "" or something, I think the simple code you had would have worked: 如果它已用“”或其他东西初始化,我认为你所拥有的简单代码会起作用:

$scope.info = ""; // don't leave it as undefined
$scope.info = $scope.info + "content updated";

With that said, you should be using an ng-repeat, in my opinion, to list out the messages. 话虽如此,在我看来,你应该使用ng-repeat来列出消息。

For example, if instead of just appending the string, you could do this in your controller: 例如,如果不是仅添加字符串,则可以在控制器中执行此操作:

$scope.info = []; // empty to start

Then you would add new messages with some controller method: 然后,您将使用一些控制器方法添加新消息:

$scope.addMessage = function(msg) {
    $scope.info.push(msg)
}

Then in your view/HTML, you would use ngRepeat: 然后在您的视图/ HTML中,您将使用ngRepeat:

<div class="info-window">
    <p ng-repeat="item in info track by $index">{{item}}</p>
</div>

The track by clause allows for duplicate messages. track by子句允许重复消息。

Update : If the items in $scope.info were actually objects and you wanted to iterate over their properties, which is what I think you ask for in the comment, then you might do something like this. 更新 :如果$ scope.info中的项目实际上是对象,并且您想迭代它们的属性,这是我认为您在评论中要求的内容,那么您可能会执行类似这样的操作。 This is getting beyond the scope of the original question, though: 但这超出了原始问题的范围:

<div class="info-window">
    <p ng-repeat="item in info track by $index">
        <div ng-repeat="(key, value) in item">{{key}} -> {{value}}</div>
    </p>
</div>

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

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