0

几个小时以来,我一直在为这个特殊的测试挠头。我相信演示的指令应该可以工作,但是对于传递给隔离范围的值,我没有定义。

代码:

html

<!doctype html>
<html ng-app="myApp">
<body>
    <a ng-href="#" class="test-scope" name="test">Console should say 'test' but gives undefined</a>
</body>
</html>

javascript

angular.module('myApp', [])
    .directive('testScope', function() {
        return {
            restrict: 'C',
            scope: {
                name: "="
            },
            link: function(scope, element, attrs) {
                console.log(scope.name);
            }
        };
    });

这是小提琴:http: //jsfiddle.net/jPtb3/8/

任何帮助将不胜感激,我一定做错了什么。

4

2 回答 2

1

您正在错误地解释隔离范围构造。

当您说name=指令声明 html 中有同名name的属性并且存在任何属性值时,请绑定到范围内的该属性名称。

在这种情况下,声明指令的范围内应该有一个范围属性名称test。指令范围上的name属性将指向父级上的这个测试属性(不是字面意思)

底线你的指令范围name- > 容器范围test属性。

在这里查看我的 plunker http://jsfiddle.net/cmyworld/ANzUf/

于 2013-10-03T04:34:14.967 回答
1

更改=@

angular.module('components', [])
.directive('testScope', function() {
    return {
        restrict: 'C',
       scope: {
            name: "@"
        },
        link: function(scope, element, attrs) {
            console.log(scope.name);
        }
    };
})

angular.module('myApp', ['components']);

见固定Fiddle

@– 将父范围属性的值(始终为字符串)绑定到本地范围。

=– 直接绑定父范围属性,在传入之前将对其进行评估。

于 2013-10-03T04:36:24.393 回答