为什么此代码在AngularJS 1.2中不起作用

Why this code does not works in AngularJS 1.2?

本文关键字:不起作用 AngularJS 代码 为什么      更新时间:2023-09-26

我有用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);   
            }
        }
    }])
;

作用域具有title属性,但未呈现。为什么?

如果我将指令配置更改为scope:true,它将正常工作: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);   
            }
        }
    }])
;

这是Angular 1.2中的一个错误或功能?旧版本在所有这些情况下都可以正常工作:http://jsfiddle.net/VmkQy/3/

<span />内部的{{title}}将被替换。将template: "{{title}}"添加到您的指令中,它就起作用了:

http://jsfiddle.net/VmkQy/5/