我创建了两个示例:
坏了
这就是我创建<hello-world>指令的方式(与此How can I define my controller using TypeScript?相关)
module HelloWorld
{
var app = angular.module('TheApp');
export class HelloWorldDirective implements ng.IDirective
{
public restrict: string = "E";
public replace: boolean = true;
public template: string = "<div>" +
"<input ng-model=\"vm.MyValue\" />" +
"<button ng-click=\"vm.Show()\" >Show my value</button>" +
"<p> my value <b>{{vm.MyValue}}</b></p>" +
"</div>";
public controller: string = 'HelloWorldCtrl';
public controllerAs: string = 'vm';
//public scope = {};
}
app.directive("helloWorld", [() => new HelloWorld.HelloWorldDirective()]);
export interface IHelloWorldScope extends ng.IScope
{
// properties for isolated scope
}
export class HelloWorldCtrl
{
static $inject = ["$scope"];
constructor(protected $scope: HelloWorld.IHelloWorldScope){ }
public MyValue: string = "InitValue";
public Show(): void
{
alert(this.MyValue)
}
}
app.controller('HelloWorldCtrl', HelloWorld.HelloWorldCtrl);
}
这是 <other-directive> TypeScript 代码:
module OtherDirective
{
var app = angular.module('TheApp');
export class OtherDirectiveDirective implements ng.IDirective
{
public restrict: string = "E";
public replace: boolean = true;
public template: string = "<h4>" +
"OtherValue: {{vm.OtherValue}}" +
"</h4>";
public controller: string = 'OtherDirectiveCtrl';
public controllerAs: string = 'vm';
//public scope = {};
}
app.directive("otherDirective", [() => new OtherDirective.OtherDirectiveDirective()]);
export interface IOtherDirectiveScope extends ng.IScope
{
// properties for isolated scope
}
export class OtherDirectiveCtrl
{
static $inject = ["$scope"];
constructor(protected $scope: OtherDirective.IOtherDirectiveScope) {}
public OtherValue: string = "This is other Value";
}
app.controller('OtherDirectiveCtrl', OtherDirective.OtherDirectiveCtrl);
}
所以,如果我们将这两个并排放置:
<div>
<hello-world></hello-world>
<other-directive></other-directive>
</div>
<hello-world> 不起作用。它会被打破。原因是“共享”,不是独立作用域,两个指令都可以访问。
让它工作
要修复它,我们必须同时更改(在这个简单的示例中至少要更改一个)。 或者为controllerAs使用不同的名字。
查看文档 (Angular Directive Guide)
隔离指令的范围
我们上面的 myCustomer 指令很棒,但它有一个致命的缺陷。我们只能在给定范围内使用一次。
...
我们希望能够做的是将指令内部的作用域与外部作用域分开,然后将外部作用域映射到指令的内部作用域。我们可以通过创建我们所说的隔离作用域来做到这一点。为此,我们可以使用指令的范围选项:
这里我们将要求隔离范围:
export class HelloWorldDirective implements ng.IDirective
{
public restrict: string = "E";
public replace: boolean = true;
public template: string = "<div>" +
"<input ng-model=\"vm.MyValue\" />" +
"<button ng-click=\"vm.Show()\" >Show my value</button>" +
"<p> my value <b>{{vm.MyValue}}</b></p>" +
"</div>";
public controller: string = 'HelloWorldCtrl';
public controllerAs: string = 'vm';
// isolated scope requested
public scope = {};
}
export class OtherDirectiveDirective implements ng.IDirective
{
public restrict: string = "E";
public replace: boolean = true;
public template: string = "<h4>" +
"OtherValue: {{vm.OtherValue}}" +
"</h4>";
public controller: string = 'OtherDirectiveCtrl';
public controllerAs: string = 'vm';
// isolated scope requested as well
public scope = {};
}
在行动中检查它here
有关此方法的更多信息: