0

html :

   <div ng-app="appMod">
    <div task-info>{ { data.name } }</div>
   </div>

script :

var appmod = angular.module('appMod', []);
appmod.directive("taskInfo", function () {
    return {
        restrict: 'A',
        scope: {},
        link: function ($scope, $element, attr) {

            $scope.taskdat = '{"name":"Task name","status":"Completed"}';
            $scope.data = JSON.parse($scope.taskdat);
            scope = $scope; //scope data
        },
    };
});

is it possible to bind directive scope without having controller scope in Angular Js? If yes, please give me some solution examples.

1
  • I think you just provided one Commented Feb 26, 2015 at 7:52

2 Answers 2

1

You don't need a controller scope for writing a directive , see this fiddle.

Here, there is no controller scope, and the value hero is bound within the directive as:

myApp.directive('myDirective', function() {
  return {
        restrict: 'EAC',
        link: function($scope, element, attrs, controller) {
          var controllerOptions, options;


            $scope.hero='superhero'
        }
      };

});

Works fine :)


Also the example you provided is similar, but you just need to remove scope from returned JSON object(from directive), as it is being defined as $scope inside the link fucntion.

see : http://jsfiddle.net/bg0L80Lx/

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

Comments

0

controller option ?

.directive('mydirective', function() {
    return {
restrict: 'A', // always required
//controller: 'SomeController'
template:'<b>{{status}}</b>',
controller:'YourCtrl'
 }
})

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.