2014-04-26 139 views
6

我如何可以添加自定义字段的角度范围与通过字段属性一起,如下:AngularJs定制指令隔离范围的自定义字段

angular.module('app') 
    .directive("myDirective", function(){ 
     function NewObj() 
     { 
      this.id = 0; 
      this.name = ""; 
     } 
     return{ 
      restrict: "E", 
      templateUrl:"partials/directives/temp.html", 
      scope:{ 
        viewId:"=", 
        dataObj: new NewObj() //This is the custom obj 
        } 

      } 
     } 

当我这样做,我得到无效的隔离范围定义。 这怎么可能被帮助?

+0

下面是关于指令范围像样的解释:http://umur.io/angularjs-directives-using-isolated-scope-with-attributes/ –

回答

5

指令中的范围只能是'=','&','@'中的一个。 做你想做什么,你可以尝试这样的:

angular.module('app') 
.directive("myDirective", function() { 
    function NewObj() { 
     id = 0; 
     this.name = ""; 
    } 
    return { 
     restrict: "E", 
     templateUrl:"partials/directives/temp.html", 
     scope: { 
      viewId:"=",      
     }, 
     controller: ['$scope', function($scope) { 
      $scope.dataObj = new NewObj(); 
     }] 
    }; 
}); 
+0

不幸的是,这不会让我重复使用同一个控制器(在范围中传递不同的参数)来执行多个指令。 –

相关问题