Por que esse código não funciona no AngularJS 1.2?
-
20-12-2019 - |
Pergunta
Eu tenho este código, escrito com Angular 1.2: http://jsfiddle.net/VmkQy/1/
<div ng-app="app">
Title is: <span my-directive data-title="Test title">{{ title }}</span>
</div>
angular.module('app', [])
.directive('myDirective', [function() {
return {
restrict: 'A',
scope: {title:'@'},
link: function($scope) {
alert($scope.title);
}
}
}])
;
O escopo tem um title
propriedade, mas não é renderizada.Por que?
Se eu alterar a configuração da diretiva para scope:true
, funcionará bem: http://jsfiddle.net/VmkQy/2/
angular.module('app', [])
.directive('myDirective', [function() {
return {
restrict: 'A',
scope: true,
link: function($scope, $element, attrs) {
$scope.title = attrs.title;
alert($scope.title);
}
}
}])
;
Este é um bug ou recurso do Angular 1.2?A versão mais antiga funciona bem em todos estes casos: http://jsfiddle.net/VmkQy/3/
Solução
O {{title}}
dentro do seu <span />
é substituído.Adicionar template: "{{title}}"
à sua diretiva e funciona:
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow