9

Is there any way you can bind/share the same scope/controllers/directives and stuff into another iframe in the same domain?

would be cool if i could have the same ngApp in another iframe aswell. Has anyone done anything like this before?

1 Answer 1

11

Yes it is possible and you don't need to do anything special, you just need to compile the content.

Here is an example: http://jsfiddle.net/gWgYM/

HTML:

<div ng-app="app" ng-controller="test">
    <h1>The date is {{date}}</h1>
    <iframe frame=""></iframe>
</div>

Javascript:

var app = angular.module('app', []);

app.controller('test', function( $scope ) {
    $scope.date = new Date();
    window.setInterval(function() {
        $scope.date = new Date();
        $scope.$apply();
    }, 1000);
});

app.directive('frame', function( $compile ) {
    return function( $scope, $element ) {
        var $body = angular.element($element[0].contentDocument.body),
            template  = $compile('<p>The date in the iframe is {{date}}</p>')($scope);
        $body.append(template);
    };
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.