1

I am trying to add a template with a controller using a directive:

<div directOne>
</div>  

Directive:

a.directive("direct-one", function ($templateRequest, $compile, $controller) {
return {
    link: function (scope, elem, attrs) {
        $templateRequest("template.html").then(function (html) {
            var $scope = scope.$new();
            $controller('oneController', {$scope, $scope});
            var template = angular.element(html);
            $(elem).append(template);
            template = $compile(template)(scope);
        });
    }
  };
});

template:

<span ng-bind="test"></span>
<div ng-controler="oneController">
</div>

controller:

app.controller('oneController', function () {
   $scope.test = '123456';
}

My problem that is the test value is not shown.

I have seen some working samples of what I am doing. My real code is more complex.

Hope, someone can help me.

Thanks

Update 1 My code should look like this then:

<div  style="width:90%;height:100%">
     <div ng-controler="oneController" directOne>        
</div>    

And that code is a part of another template with its own controller. So, unless I have $scope.test on the parent controller the data is not shown.

1

2 Answers 2

1

You need to wrap the Controller div around anything that accesses your controller. So it should be:

 <div ng-controler="oneController">
     <span ng-bind="test"></span>
 </div>
Sign up to request clarification or add additional context in comments.

Comments

1

You need to do exactly opposite of what you are currently doing.

While defining directive you should name it as directOne & then use that directive with - separated value instead of Upper case letter like direct-one.

ng-bind="test" value haven't evaluated because you attached controller to directive DOM.

For fixing your issue, you need to compile your new template.html element with $controller('oneController', {$scope, $scope}); scope, then will be able to evaluate the value of test variable.

Otherwise you could easily fix this by using directive controller option.

Markup

<div direct-one></div>

Directive

a.directive("directOne", function ($templateRequest, $compile, $controller) {
return {
    //controller: 'oneController',
    link: function (scope, elem, attrs) {
        $templateRequest("template.html").then(function (html) {
            var $scope = scope.$new();
            //stored oneController scope in variable
            var controllerScope = $controller('oneController', {$scope, $scope});
            var template = angular.element(html);
            $(elem).append(template);
            template = $compile(template)(controllerScope); //compiled with oneController scope
        });
    }
  };
});

1 Comment

@Mark do look at updated answer..that most probably help you.. Thanks :)

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.