角度属性指令值

nan*_*doj 8 javascript angularjs

我想直接从属性指令获取一个值:

 <form cronos-dataset="People as p">
     Form Content
 </form>
Run Code Online (Sandbox Code Playgroud)

在我的JS中我尝试过:

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

.controller("CronosGenericDatasetController",['$scope', function($scope) {
    alert($scope["cronos-dataset"]);
}]);
Run Code Online (Sandbox Code Playgroud)

我想提醒"People as p"字符串,但我明白了undefined.这是正确的道路还是我应该采取不同的方法?

Har*_*ryH 8

你应该在范围声明中有camelCase

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

这是一个演示,可以看到不同的变化 http://plnkr.co/edit/G6BiGgs4pzNqLW2sSMt7?p=preview


m0m*_*eni 5

改为建立链接功能:

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