كيف يمكنني إخراج علامة البرنامج النصي المخزنة كسلسلة في نطاقي إلى قالب AngularJS؟

StackOverflow https://stackoverflow.com//questions/25053926

سؤال

أحتاج إلى استخدام برنامج نصي مضمن في القوالب الخاصة بي ولكن أقوم بإدراج مسار إلى صورة ديناميكيًا قبل تشغيل البرنامج النصي.

لقد كتبت توجيهًا لبناء مسار الصورة كمتغير نطاق، ثم استخدمه $interpolate لإدراج هذا في <script> بطاقة شعار.

المرجع المخزن الآن (كسلسلة) في $scope.tag هو الكود الذي أرغب في إدخاله وتفسيره/تشغيله كما تفعل علامة البرنامج النصي.

بدلاً من ذلك، تمت إضافة علامة البرنامج النصي كسلسلة في القالب الخاص بي ولا يبدو أنني أستطيع تحديد الخطوة الإضافية التي يجب علي اتخاذها لإخراج سلسلة علامة البرنامج النصي فعليًا كعلامة برنامج نصي.

هنا هو القالب (المشار إليه في التوجيه باسم templateUrl):

<div>
  {{ tag }}
</div>

التوجيه:

'use strict';

angular.module('App')
  .directive('sslLogo', function($location, $interpolate) {
    return {
      replace: false,
      restrict: 'AE',
      templateUrl: '/views/partials/ssl-logo.html',
      controller: function($scope, $element, $attrs) {
        // Outputs the full base url (http:// ... .com/ )
        var base_url = '';
            base_url += $location.protocol() + '://';
            base_url += $location.host();

            if ( $location.port() ) {
              base_url += ':' + $location.port();
            }

        // Add the path to the image asset.
        $scope.logo_url = base_url + "/images/static/comodo-wildcardssl.png";

        // Interpolate this path into the script tag and then store the script tag to be output to the template.
        $scope.tag = $interpolate('<script type="text/javascript">TrustLogo("{{ logo_url }}", "SCCC", "none");</script>', false, true)({ logo_url: $scope.logo_url });
      },
      link: function(scope, element, attrs) {}
    };
  });

للإشارة، في هذه المرحلة $scope.tag سيحتوي المتغير على سلسلة من:

<script type="text/javascript">TrustLogo("http://localhost:9000/images/static/comodo-wildcardssl.png", "SCCC", "none");</script>
هل كانت مفيدة؟

المحلول

يبدو أن:

...
link: function(scope, element, attrs) {
  element.append( scope.tag );
}
...

الأعمال التي تستخدم jQuery لإلحاق علامة البرنامج النصي بعنصر التوجيه.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top