Form Content...">

Angular attribute attribute value

I want to get the value directly from the attribute directive:

<form cronos-dataset="People as p"> Form Content </form> 

In my JS, I tried:

 app.directive('cronosDataset',[function() { return { restrict: 'A', controller: 'CronosGenericDatasetController', scope: { "cronos-dataset" : '@' } }; }]) .controller("CronosGenericDatasetController",['$scope', function($scope) { alert($scope["cronos-dataset"]); }]); 

I want to report the string "People as p" , but I get undefined . Is this the right way or should I continue with a different approach?

+6
source share
2 answers

It is assumed that camelCase is in the scope declaration

 app.directive('cronosDataset',[function() { return { restrict: 'A', controller: 'CronosGenericDatasetController', scope: { cronosDataset : '@' } }; }]) 

Here is a demo to see the different options http://plnkr.co/edit/G6BiGgs4pzNqLW2sSMt7?p=preview

+7
source

Instead, make a link:

 app.directive('cronosDataset',[function() { return { scope: {}, restrict: 'A', link: function (scope, elem, attrs) { alert(attrs.cronosDataset); } 
+4
source

Source: https://habr.com/ru/post/983323/


All Articles