Greetings

Please tell me how to transfer data from a controller to a directive using the syntax controller as

For example:

 angular .module('test', []) .controller('first', function () { this.somevar = 'Lorem'; // -> Эту переменную }) .directive('one', function () { restrict: 'A', link: function () { console.log(somevar); // <- Получить здесь } }); 
  • a bunch of options, starting with the transfer in the markup, and ending with the receipt of the Osprey - Grundy
  • @Grundy, I would be happy to have a couple of options or a link with them) - Nikolay
  • e.g. jsfiddle.net/joshdmiller/FHVD9 - Kostiantyn Okhotnyk
  • @KonstantinOkhotnick, I need not to display, but to send for further use) - Nikolay
  • @ Nikolai, well, use this scope.obj as an example - Grundy

2 answers 2

Such implicit dependencies are evil.
These are like global variables, only worse.
The directive should not depend on the controller.
It should be painlessly easy to move to another place.

  • Option 1 (so-so): A default value is added to each variable from the external scope inside the directive. It turns out something like% ENV% environment. All data is available anywhere, but the external controller can override some of them.

 angular .module('test', []) .controller('first', ['$scope', function ($scope) { $scope.somevar = 'Lorem'; }]) .directive('one', function() { var defaults = { somevar: 'Ipsum', othervar: 'Dolor' }; return { restrict: 'A', link: function(scope) { var params = {}; Object.keys(defaults).forEach(key => params[key] = scope[key] || defaults[key] ); console.log(params.somevar, params.othervar) ; } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="first as c"> <div one></div> </div> 

  • Option 2: The isolated scope of the directive, all relevant information is passed through attributes.

 angular .module('test', []) .controller('first',['$scope', function ($scope) { this.somevar = 'Lorem'; }]) .directive('one', function() { return { restrict: 'A', scope: {text: '='}, link: function(scope) { console.log(scope.text) } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="first as c"> <div one text="c.somevar"></div> </div> 

  • Option 3: Services are used for data exchange.

 angular .module('test', []) .controller('first', ['dataProvider', function (data) { data.set('somevar', 'Lorem'); }]) .service('dataProvider', function() { var data = {}; this.set = function(key, val){data[key] = val;}; this.get = function(key) {return data[key];}; }) .directive('one', ['dataProvider', function(data) { return { restrict: 'A', scope: {}, link: function() { console.log(data.get('somevar')) } }; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="first as c"> <div one></div> </div> 

  • Add an example for the first option, I do not understand what you mean - Grundy
  • A couple of minutes, I will add the third option) - vp_arth
  • @Grundy, Added an example for the first option - vp_arth
  • @vp_arth, thanks! I decided to use the service :) - Nikolay

For example:

 var myApp = angular.module('myApp',[]); myApp.directive('one', function() { return { restrict: 'A', link: function (scope) { console.log(scope.somevar); // <- Получить здесь }, }; }); myApp.controller('MyCtrl', function ($scope) { $scope.somevar = 'Lorem'; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <div one></div> </div> 

Another option:

 angular .module('test', []) .controller('first',['$scope', function ($scope) { $scope.somevar = 'Lorem'; }]) .directive('one', function() { return { restrict: 'A', link: function (scope) { console.log(scope.somevar) }, }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="first"> <div one></div> </div> 

And 1 more option

 angular .module('test', []) .controller('first',['$scope', function ($scope) { this.somevar = 'Lorem'; }]) .directive('one', function() { return { restrict: 'A', controller: 'first', link: function(scope, element, attrs, ctrl) { console.log(ctrl.somevar) } }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="test" ng-controller="first"> <div one="somevar"></div> </div> 

  • Pay attention that the question concerns the controller as syntax, only the field in the controller, and not directly in the Osprey - Grundy
  • @Grundy, is it possible in more detail? =) - Nikolay
  • this.somevar = 'Lorem'; , if you make this.somevar = 'Lorem'; in the controller this.somevar = 'Lorem'; then in the directive you will not get it as a simple scope.somevar = 'Lorem'; - Grundy
  • @Grundy, isn't that a mix of syntaxes? And how bad is it to do that? - Nikolay
  • @Nikolai, I don’t know what syntax mixing is . An example that I gave - just will not work - Grundy