0

问题:我创建了一个带有按钮的 md 卡的网页。单击该按钮后,我希望出现另一张卡片。问题:我不知道如何使新卡的按钮起作用。它的格式不正确,没有任何功能。

问题:我需要将代码更改为什么才能使其正常工作?谢谢。

代码:这是我的 JavaScript 代码:

angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
    .controller('AppCtrl', function($scope, $mdDialog) {

    function addMeinElement () { 
                                // create a new div element 
                                var newDiv = document.createElement("md-card");
                                document.querySelector('#myID').appendChild(newDiv);
                                newDiv.innerHTML = "<md-card-content>Oh no! Why does this button not work?<br>"+
                                                    "<md-button class='md-warn' ng-click='my_print_to_console()'>Button :( </md-button>"+
                                                    "</md-card-content>";
                                }

    $scope.my_print_to_console = function ()    {
                                                addMeinElement ();
                                                };  


});

这是对应的html:

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="https://www.gstatic.com/firebasejs/5.8.4/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.8.4/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/5.8.4/firebase-firestore.js"></script>

    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
    <link rel='stylesheet' href='https://cdn.gitcdn.link/cdn/angular/bower-material/v1.1.13/angular-material.css'>
    <link rel='stylesheet' href='https://material.angularjs.org/1.1.13/docs.css'>   
</head>

<body>
    <div ng-controller="AppCtrl" ng-app="MyApp">
        <div style="position:fixed; width:100%; top:0;   height:100%;"> 
            <div style="position:fixed;width:80%;top:0;left:50%;transform: translate(-50%, 0);">
                <md-card>
                    <md-card-content>
                        <p>Click the button to hopefully create a new card with another button.</p>
                            <md-button class="md-warn" ng-click="my_print_to_console()";>Button :)</md-button>
                    </md-card-content>
                </md-card>
                <div id="myID">
                </div>
            </div>
        </div>
    </div>  

<!--
Copyright 2018 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
-->

    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-route.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.min.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.min.js'></script>
    <script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.js'></script>
    <script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/t-114/svg-assets-cache.js'></script>
    <script src='https://cdn.gitcdn.link/cdn/angular/bower-material/v1.1.13/angular-material.js'></script>
    <script src="app.js"></script>
</body>

4

1 回答 1

1

发生这种情况是因为 Angular 不知道这是一个 Angular 组件。您需要使用$compile服务包装您的 HTML。

  1. 将“$compile”添加到您的模块依赖项中
  2. 像这样包装代码

$compile('你的 html 在这里')($scope);

于 2019-03-18T08:53:16.633 回答