简体   繁体   中英

Angular attribute directive value

I want to get a value straight from an 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 alert "People as p" string but I get undefined . Is that right path or should I go thorough a different approach?

You are supposed to have camelCase in the scope declaration

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

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

Make a link function instead:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM