2013-12-09 2 views
0

Я не могу использовать атрибут scope в моем представлении, который явно доступен в переменной области видимости.AngularJS1.2 Директива двухсторонняя привязка Атрибут не отражен для просмотра

Пожалуйста, смотрите plnkr: http://plnkr.co/edit/aoRU6YDywA0Q25gKpQGz?p=preview

script.js

// Code goes here 
angular.module('myapp', [ 
    'myapp.directive', 
    'myapp.controller' 
]); 

angular.module('myapp.controller', []).controller('mylist', function($scope) { 
    'use strict'; 
    $scope.mylist = [{ 
    name: "peter", 
    likes: 10 
    }, { 
    name: "bob", 
    likes: 2 
    }]; 
    $scope.testme = 'fdsds'; 
}); 

angular.module('myapp.directive', []).directive('pmTable', function factory() { 
    var directiveDefinitionObject = { 
    scope: { 
     data: '=myvar' 
    }, 
    controller: function($scope, $element, $attrs, $transclude) { 
     // console.log($scope); 
    }, 
    compile: function compile(tElement, tAttrs, transclude) { 
     // Note: The template instance and the link instance may be different objects if the template has been cloned. 
     // For this reason it is not safe to do anything other than DOM transformations that apply to all cloned DOM nodes within the compile function. 
     // Specifically, DOM listener registration should be done in a linking function rather than in a compile function. 

     console.log('inside compile'); 
     return { 
     pre: function preLink(scope, iElement, iAttrs, controller) { 
      console.log('preLink'); 
      console.log(scope); 
      console.log(scope.data); // available here 
      scope.testme = 'testhere'; 
      // scope.$apply(); // using that one doesn change anything 
     }, 
     post: function postLink(scope, iElement, iAttrs, controller) { 
      console.log('postLink'); 
      console.log(scope); 
      console.log(scope.data); // available here 
      console.log(scope.testme); // available from the parent scope 
      // scope.$apply(); // using that one doesn change anything 
     } 
     }; 
    } 
    }; 
    return directiveDefinitionObject; 
}); 

index.html

<!DOCTYPE html> 
<html data-ng-app="myapp"> 

    <head> 
    <script data-require="[email protected]" data-semver="1.2.3" src="http://code.angularjs.org/1.2.3/angular.js"></script> 
    <link rel="stylesheet" href="style.css" /> 
    <script src="script.js"></script> 
    </head> 

    <body> 
    <div ng-controller="mylist"> 
    {{mylist|json}} 
    {{ testme }} 
    <h1>Hello Plunker!</h1> 
    <div pm-table myvar="mylist"> 
     From parent Scope: {{ testme }} <br /> 
     {{myvar|json}}<br /> 
     {{data|json}}<br /> 
     <!-- this following repeat is not working --> 
     <tr data-ng-repeat="item in data"> 
      <td>Name: {{ item.name }}</td> 
     </tr> 
    </div> 
    </div> 
    </body> 

</html> 

ответ

0

pm-table является атрибутом. Объем директивы ограничивается этим атрибутом (или элементом), поскольку вы решили пойти с изолированной областью. console.log(scope.testme); выводит значение testhere, которое исходит из области действия директивы.

From parent Scope: {{ testme }} <br /> будет печатать fdsds, как указано в области контроля. Директива здесь не применяется. То же самое можно сказать и о ng-repeat. data находится в области действия директивы, которая здесь не применяется.

Простейшим решением будет унаследованная область с scope: true. Все остальное потребует дополнительной работы.

Смежные вопросы