Why are my AngularJS directives sharing scope?

后端 未结 4 456
不知归路
不知归路 2020-12-15 21:29

I\'ve tried to make a simple directive which displays a name and allows it to be change. When I put multiple directive on the name page they all seem to share the name attri

4条回答
  •  悲哀的现实
    2020-12-15 21:55

    By default, directives share the same scope. But if needed, you can use isolated scope for your directives: use scope: {} as field in your directive definition.

    app.directive('person', function () {
    
        function link ($scope, elem, attrs, ctrl) {     
    
            $scope.name = "OLD"        
    
            $scope.setName = function() {
                $scope.name = 'NEW';
            }
        }
    
        return {
          restrict: 'E',
          scope: {}
          replace: true,
          template: "Current name = {{name}}Change name
    ", link : link, } });

提交回复
热议问题