在带有text/ng模板的script标记中使用ng控制器

use ng-controller in script tag with text/ng-template

本文关键字:ng 控制器 script text      更新时间:2023-09-26

我正在尝试在脚本中使用ng-controller标记witch是一个模板,具有ng-template指令我不知道为什么现在能正常工作

<script type="text/ng-template" id="modal-5" >
<div ng-controller="reserveModalCtrl">
    <div class="modal-header">
        <button type="button" class="close" ng-click="currentModal.close();"
                aria-hidden="true">&times;</button>
        <h4 class="modal-title">Rserve Shortcode</h4>
    </div>
    <div class="modal-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    <label for="field-1" class="control-label">Shortcode</label>
                    <input type="text" class="form-control" id="field-1" 
                                 value="30570" disabled>
                </div>
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    <label for="field-2" class="control-label">Company</label>
                    <div id="field-2">
                        <select class="form-control" 
                                ng-model="currentShortCode.companyId">
                            <option ng-repeat="company in companies track by $index" 
                                   value="{{company._id}}"> {{company.companyName 
                                   + ' (' + company.companyLatinName + ')'}}</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="control-label">Until</label>
                <div>
                    <div class="date-and-time">
                        <input type="text" ng-model="currentShortCode.date" 
                               class="form-control datepicker" 
                               data-format="D, dd MM yyyy">
                        <input type="text" ng-model="currentShortCode.time" 
                               class="form-control timepicker" data-template="dropdown" 
                               data-show-seconds="true" data-default-time="11:25 AM" 
                               data-show-meridian="true" data-minute-step="5" 
                               data-second-step="5" />
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-white" 
                ng-click="currentModal.close();">Close</button>
        <button type="button" class="btn btn-info" 
                ng-click="currentModal.dismiss();">Reserve</button>
    </div>
</div>

和我的控制器

app.controller('reserveModalCtrl', function ($scope, ShortCodeService) {
    $scope.companies    = ShortCodeService.companies;
    $scope.shortCodes   = ShortCodeService.shortCodes;
    $scope.test         = 'test';
});

当我在div元素上使用它时,它是有效的,但在本例中不是。

脚本指令的文档说明您可以使用它来:

将元素的内容加载到$templateCache中,以便ngIncludeingView指令可以使用该模板。元素的类型必须指定为text/ng模板,并且必须通过元素的id为模板分配缓存名称,然后该id可以用作指令的templateUrl。

因此,要真正呈现模板,您必须在配置路由器时将其映射到路由(标准ngRoute或ui路由器),或者将其包含在ngInclude中(例如,<div ng-include="modal-5"></div>),或者作为指令定义对象的templateUrl属性。