How directives are invoked If an element that contains a directive is added dynamically through an angular controller?

前端 未结 1 1552
心在旅途
心在旅途 2021-01-24 17:23

After doing some search I haven\'t found much information in this bad practice. Let\'s say I have a controller that behaves like that (I know it should have been a directive and

1条回答
  •  时光说笑
    2021-01-24 18:08

    Based on Mohammad Shahrouri's comment above, I had to inject the $compile dependency in the controller and I had to add $compile(input)($scope); at the end:

    angular.module('app').controller('test', ['$scope','$compile',
        function($scope, $compile) {
            $scope.addElement = function() {
    
                var input = document.createElement('input');
                input.type = "text";
                //contains directive
                input.setAttribute("autosize","autosize");
                input.setAttribute("ng-model","dummy");
                //[ append code ]
                input.focus();
                $compile(input)($scope);
    
            }
        }
    ]);
    

    0 讨论(0)
提交回复
热议问题