How to set an iframe src attribute from a variable in AngularJS

后端 未结 6 1181
旧时难觅i
旧时难觅i 2020-11-22 17:24

I\'m trying to set the src attribute of an iframe from a variable and I can\'t get it to work...

The markup:

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 17:47

    select template; iframe controller, ng model update

    index.html

    angularapp.controller('FieldCtrl', function ($scope, $sce) {
            var iframeclass = '';
            $scope.loadTemplate = function() {
                if ($scope.template.length > 0) {
                    // add iframe classs
                    iframeclass = $scope.template.split('.')[0];
                    iframe.classList.add(iframeclass);
                    $scope.activeTemplate = $sce.trustAsResourceUrl($scope.template);
                } else {
                    iframe.classList.remove(iframeclass);
                };
            };
    
        });
        // custom directive
        angularapp.directive('myChange', function() {
            return function(scope, element) {
                element.bind('input', function() {
                    // the iframe function
                    iframe.contentWindow.update({
                        name: element[0].name,
                        value: element[0].value
                    });
                });
            };
        });
    

    iframe.html

       window.update = function(data) {
            $scope.$apply(function() {
                $scope[data.name] = (data.value.length > 0) ? data.value: defaults[data.name];
            });
        };
    

    Check this link: http://plnkr.co/edit/TGRj2o?p=preview

提交回复
热议问题