简体   繁体   中英

Dynamically updating the template's Html in an Angular directive

I have a custom directive called customStyles, which is defined as :

scrollbackApp.directive('customStyles', function(){
    return{
        restrict: 'E',
        template: '<style> {{styleString}} </style>',
        scope: {
            conversations : '='
        },
        link: function($scope, elem, attrs){
            $scope.$watch('conversations', function(value){
                // calculate str based on value
                $scope.styleString = str;
            });
        }
    }
});

I'm adding this directive to the body of my Html view:

<body>
  <custom-styles conversations="convList"> </custom-styles>
</body>

And the value of convList changes inside the parent controller scope. As present, when the above directive renders to Html, the {{styleString}} binding remains as a string, instead of changing into it's value. I want the html of the directive to change dynamically based on the value of styleString . Is this possible in Angular?

You should rename directive tag in view:

<custom-styles conversations="convList"> </custom-styles>

Here is working example:

http://plnkr.co/edit/aOxf3fgS7Tc0sOyCXoBV?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM