0

I am adding Angular directive content dynamically, but I'm not able to add functionality like $scope and Controller inside it. How do I solve this issue? For example:

a.html

<div ng-controller="actrl">{{aname}}</div>

b.html

<div ng-controller="bctrl">{{bname}}</div>

Suppose that I have a directive with the template URL: /a.html and I change it to /b.html dynamically, then for b.html, the angular functionalities (bctrl) are not working.

jQuery

jQuery.ajax({
  url: menu.templateUrl,
  success: function(response) {                   
    jQuery("view-partial").html(response);                      
  }
});

4 Answers 4

1

Include content like this using ng-view

 $routeProvider
      .when('#/a.html', {
        templateUrl: 'b.html',
        controller: 'bctrl',

      });

Here is solution for your issue check the link http://jsbin.com/voyeki/2/edit

I am complied your code with

.controller("myCtrl",function($scope,$compile){
angular.element("view-partial").html($compile(response)($scope));
});

Hope this is solution for your issue :-)

Sign up to request clarification or add additional context in comments.

7 Comments

is there any other way? because if i switch to routing we have to change lot of coding.
peter.grman.at/dynamic-controllers-in-angularjs you need to manually create scope object. check the link hope helps for your requirement
It not possible to create fiddle . because many things depends on dynamic. can you please look at this code ...how can i bind instead using jQuery("view-partial").html($scope.data); -------------------------$http({ url: menu.templateUrl, method: "GET" }).success(function(data, status) { $scope.data = data; jQuery("view-partial").html($scope.data); }).error(function(data, status) { $scope.status = status; });
Thanks @sarath .Now it's working, controller code main js file. If the controller code in partial page it's not working. please help me to resolve this issue. example- <div ng-controller="Controller2"> <h1>Hello {{support.name}}</h1> </div> <script> bosAppModule.controller('Controller2', ['$scope', function($scope) { $scope.support = { name: 'bagya', address: 'bangalore' }; }]); </script>
Write controller2 script code in external js file or all controllers code in same page
|
0

No need to use $scope on the view :

<div ng-controller="actrl">{{$scope.aname}}</div>

You need to use directly aname

<div ng-controller="actrl">{{aname}}</div>

2 Comments

HTML Code: <view-partial></view-partial> JS Code: jQuery.ajax({url: menu.templateUrl, success: function(response) { jQuery("view-partial").html(response); } });
you need to take a look at angular routing, of course it will not work because angular isn't aware of the changes you made using jquery... I suggest you to use angular routing and don't use jquery for the logical aspect of your app.
0
HTML Code:
 <view-partial></view-partial>  
JS Code: 

jQuery.ajax({url: menu.templateUrl,
            success: function(response) {                   
            jQuery("view-partial").html(response);                      
                    }
        });

Comments

0

Main.js for example

   <script>  

        var bosAppModule = angular.module("bosApp", []); 

        $http({
                 url: menu.templateUrl,
                  method: "GET"              
             }).success(function(data, status) {
              $scope.data = data;
             jQuery("view-partial").html($compile($scope.data)($scope));
                              }).error(function(data, status) {
                               $scope.status = status;
            });
        bosAppModule.controller('Controller2', ['$scope', function($scope) {
            $scope.support = {
              name: 'bagya',
              address: 'bangalore'
            };   
        }]);    
    </script>
Partial page
  <div  ng-controller="Controller2">
    <h1>Hello {{support.name}}</h1>
    </div>

Now it's working, if the controller code main js file. If the controller code in partial page it's not working. please look out below code and help me to resolve this issue.

example

<div  ng-controller="Controller2">
<h1>Hello {{support.name}}</h1>
</div>
<script>
bosAppModule.controller('Controller2', ['$scope', function($scope) {
    $scope.support = {
      name: 'bagya',
      address: 'bangalore'
    };   
}]);
</script>

Actually i need evey controller in that own partial page.

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.