【问题标题】:Angular Isolated scope not binding角度隔离范围不绑定
【发布时间】:2015-07-15 18:07:33
【问题描述】:

我有一个如下创建的指令和下面的控制器,但我似乎可以让父范围工作

"use strict";
app.directive("ccDetails", function () {
    return {
        restrict: "E",
        replace: true,
        templateUrl: "/give/creditcarddetails",
        scope: {
            showCreditCardDetails: "=showCreditCardDetails",
            creditCardDetails: "=",
            nameOnCard: "="
        },
        controller: function ($scope) {
            console.log("Name on card is " + $scope.nameOnCard);
            console.log("showCreditCardDetails is " + $scope.showCreditCardDetails);
            console.log("creditCardDetails is " + $scope.creditCardDetails);
        }
    }
});

我正在动态编译并将其附加到我的 DOM 的一部分。

"use strict";
app.controller("giveController", [
    "$scope", "$compile", function ($scope, $compile) {
        $scope.donationTypes = [
            { id: 1, name: "Credit Card" }
        ];

        $scope.donationTypeChange = function () {

            
            $scope.showCreditCardDetails = false;
            $scope.nameOnCard = "James";

            // credit card donationType
            if ($scope.selectedDonationType != null && $scope.selectedDonationType.id === 1) {
                var creditCardDetailsHtml = $compile("<cc-Details showCreditCardDetails=\"showCreditCardDetails\"></cc-Details>")($scope);
             
                $scope.showCreditCardDetails = true;
                console.log(creditCardDetailsHtml);

               $(".give-step-1").after(creditCardDetailsHtml);
               
            }
        };
    }
]);

范围不具有约束力且无法正常工作。有什么帮助吗?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    HTML 上的属性需要使用破折号而不是驼峰式大小写。还有指令本身的名称:

    "<cc-details show-credit-card-details=\"showCreditCardDetails\"></cc-details>"
    

    关于其他属性,由于你的指令有一个独立的范围,你需要通过 HTML 元素传递你想要的所有对象。

    【讨论】:

    • 除非应该是小写的“d”:&lt;cc-details&gt;
    • 或使用服务传递额外数据
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2014-07-13
    • 2015-08-02
    • 2017-09-22
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    相关资源
    最近更新 更多