Including tripadvisor widget in angularjs app

后端 未结 2 1429
悲哀的现实
悲哀的现实 2020-12-07 05:12

Hi I am new to angularjs. I want to add a tripadvisor widget to my angularjs app. The widget code is like this:

2条回答
  •  借酒劲吻你
    2020-12-07 05:18

    I tweaked the answer by drew_w a little to work with the "selfserve" review/ratings widgets TA have:

    (function() {
      'use strict';
    
      angular
        .module('myapp')
        .directive('tripadvisor', function () {
        return {
          restrict: 'E',
          scope: {
            location: '='
          },
          link: function (scope, element, attrs) {
            element.append(angular.element('
    ')); scope.$watch("location", function (n, o) { if (angular.isDefined(scope.location)) { var script = '//www.tripadvisor.com/WidgetEmbed-selfserveprop?&nreviews=5&locationId=' + scope.location + '&rating=true&border=true'; $.getScript(script, function() { if (window.taValidate) { window.taValidate(); } }); } }); } }; }); })();

    Use in your HTML:

    
    

    I've tried to follow the angular style guide for this code, hence the IIFE (that funny (function() { at the start and })(); at the end). I use the "controller as" syntax, hence the 'vm.' in the HTML.

    This is my first directive, so feedback/improvement tips are welcome! :-)

提交回复
热议问题