简体   繁体   English

模板Angularjs中的ng-repeat

[英]ng-repeat inside a template Angularjs

I have a payment directive as below. 我有如下付款指令。 A payment can have one of a number of different payment methods. 付款可以具有多种不同的付款方式之一。 There is a select list in side the template which should be from a list of payment methods that are passed in. 模板旁边有一个选择列表,该列表应该来自传入的付款方式列表。

But at the moment the select list is not being populated with anything. 但是目前,选择列表中没有任何内容。

I'm pretty new to angular. 我对棱角还很陌生。

Payment Directive 付款指令

angular.module('filanthropyApp.directives', [])
.directive('payment', function () {
return {
    restrict: 'EA',      
    templateUrl: '/Content/filanthropyApp/Directives/Templates/payment.html',
    replace: true,
    scope: {
        paymentMethods: '=paymentMethods'

    }
 };
});

Payment Template 付款模板

<div>
<div class="col-md-8 input-group">
    <label class="control-label col-md-4">Payment Amount</label>
    <input type="text" class="form-control" value="{{payment.Amount}}" ng-model="payment.Amount" />
</div>

<div class="col-md-8 input-group">
    <label class="control-label col-md-4">Payment Method</label>
    <select ng-model="payment.PaymentMethodId" name="paymentMethods" ng-options="paymentMethod.Id as paymentMethod.Name for paymentMethod in paymentMethods">
        <option value="">Select a Payment Method</option>
    </select>
</div>

Calling Page 呼叫页面

<section id="payments">

            <div class="form-group payment" ng-repeat="payment in pledgeData.Payments">

                <payment paymentMethods="pledgeData.PaymentMethods" />

            </div>

            <button class="btn btn-default" ng-click="addPayment()">Add New Payment</button>

        </section>

The attribute name is specified in the wrong way in the calling page, it should be payment-methods. 在调用页面中以错误的方式指定了属性名称,它应该是付款方式。

<payment payment-methods='pledgeData.PaymentMethods'...

Angular has this convention of using dashes in html and camelCase in the code. Angular具有在HTML中使用破折号和在代码中使用camelCase的约定。

Moreover, since you are using a key named as the attribute in the isolated scope of the directive, you can also specify it as: 此外,由于在指令的隔离范围内使用的键名为属性,因此也可以将其指定为:

scope: {
     paymentMethods: '='
}

without repeating it. 无需重复。

In your Directive, you define the scope as containing a single property called paymentMethods . 在您的指令中,您将范围定义为包含一个名为paymentMethods的单个属性。 Yet in your template, you just refer to payment . 但是,在模板中,您仅指payment Note that the inner scope of a directive cannot access the outer scope that it is used in when you define the scope attribute on the directive definition. 请注意,指令的内部作用域不能访问在指令定义上定义scope属性时使用的外部作用域。

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

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