0

我需要在指令中使用隔离范围;我正在尝试读取属性中指定的模型的值。所以我的指令是这样使用的:

<div mydirective="" mydirective-data="MyJson" />

在控制器中,我为$scope.MyJson. 我的指令应该接受它,但没有。

app.directive('mydirective', function() {
   return {
     restrict: 'A',
     scope: {
       data: '&mydirectiveData',
     },
     link: function(scope, element, attrs) {
         console.log(scope.data);
         }
     }
   }
);

请注意,我需要使用隔离范围。我还创建了一个JSFiddle来解决这个问题。(记得打开控制台)

我希望看到 的值MyJson,但我什么也没看到。

4

2 回答 2

4

用=替换&范围内的,它会得到你的对象。如果您&使用的是scope.data()

于 2013-09-13T17:32:23.300 回答
1

我会这样写我的指令:

app.directive('mydirective', function() {
   return {
     restrict: 'A',
     scope: {
       mydirectiveData: '='
     },
     link: function(scope, element, attrs) {
         console.log(scope.mydirectiveData);
         }
     }
   }
);
于 2013-09-13T17:28:30.110 回答