如何在 AngularJS 指令中绑定属性?
How to bind an attribute in a AngularJS directive?
我已经定义了一个 Angular 指令,它似乎工作正常 --- 如果我硬编码它的属性值。例如:
<notes-history application-id="11"></notes-history>
{{applicationid}}
<notes-history application-id="{{applicationid}}"></notes-history>
第一个 <notes-history>
指令工作正常。纯文本绑定工作正常。但是,当第二次调用指令的 link()
函数时,application-id(基于传递给 link()
的第三个参数)是一个空字符串。
如何让它绑定到属性?
更新:
这是该指令的(大部分)完整代码。 (Javascript 从 TypeScript 生成)
var notesHistory = (function () {
function notesHistory(notesService, alertsService) {
this.notesService = notesService;
this.alertsService = alertsService;
var directive = {};
directive.priority = 0;
directive.restrict = "EA";
directive.scope = { applicationId: "@applicationId" };
directive.replace = true;
directive.transclude = false;
directive.templateUrl = "app/views/notesHistory.html";
directive.link = function (scope, instanceElement, instanceAttributes, controller, transclude) {
// elided.
// instanceAttributes.applicationId is correct here
// when hard-code, and '' when bound.
});
};
return directive;
}
return notesHistory;
})();
app.directive("notesHistory", ["notesService", "alertsService", notesHistory]);
更新 2
我其实已经想通了我的问题,原来和指令无关
问题是控制器的第一个(基本上也是唯一的)操作是进行异步 Web 服务调用以获取数据。所以事件的顺序是:
- 运行 控制器(启动网络服务调用)
- 运行 指令(使用数据)
- 处理来自网络服务的响应(接收指令所需的数据)
由于这已成为一个完全不同的问题,我将为其创建一个新问题。
应该可以。这里有一个超级简单的 plunker 来举例说明:http://plnkr.co/edit/tYFbxxAxwLXAzaNyCOwE?p=preview
HTML
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<test foo="bar"></test>
<test foo={{name}}></test>
</body>
JS
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.directive('test', function() {
return {
scope: {
foo: '@'
},
template: 'Name: {{foo}}'
};
});
我已经定义了一个 Angular 指令,它似乎工作正常 --- 如果我硬编码它的属性值。例如:
<notes-history application-id="11"></notes-history>
{{applicationid}}
<notes-history application-id="{{applicationid}}"></notes-history>
第一个 <notes-history>
指令工作正常。纯文本绑定工作正常。但是,当第二次调用指令的 link()
函数时,application-id(基于传递给 link()
的第三个参数)是一个空字符串。
如何让它绑定到属性?
更新:
这是该指令的(大部分)完整代码。 (Javascript 从 TypeScript 生成)
var notesHistory = (function () {
function notesHistory(notesService, alertsService) {
this.notesService = notesService;
this.alertsService = alertsService;
var directive = {};
directive.priority = 0;
directive.restrict = "EA";
directive.scope = { applicationId: "@applicationId" };
directive.replace = true;
directive.transclude = false;
directive.templateUrl = "app/views/notesHistory.html";
directive.link = function (scope, instanceElement, instanceAttributes, controller, transclude) {
// elided.
// instanceAttributes.applicationId is correct here
// when hard-code, and '' when bound.
});
};
return directive;
}
return notesHistory;
})();
app.directive("notesHistory", ["notesService", "alertsService", notesHistory]);
更新 2
我其实已经想通了我的问题,原来和指令无关
问题是控制器的第一个(基本上也是唯一的)操作是进行异步 Web 服务调用以获取数据。所以事件的顺序是:
- 运行 控制器(启动网络服务调用)
- 运行 指令(使用数据)
- 处理来自网络服务的响应(接收指令所需的数据)
由于这已成为一个完全不同的问题,我将为其创建一个新问题。
应该可以。这里有一个超级简单的 plunker 来举例说明:http://plnkr.co/edit/tYFbxxAxwLXAzaNyCOwE?p=preview
HTML
<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<test foo="bar"></test>
<test foo={{name}}></test>
</body>
JS
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
});
app.directive('test', function() {
return {
scope: {
foo: '@'
},
template: 'Name: {{foo}}'
};
});