-1

我正在尝试角度指令,我很困惑。

http://jsfiddle.net/S2cQD/2/

我有四个指令:person、personWholeName、personFirstName 和 personLastName。person 调用 personWholeName 指令。personWholeName 调用 personFirstName 和 personLastName 指令。但是当我使用 person 指令时,我没有看到 personLastName 指令被渲染。

这是我的html:

<script type="text/ng-template"    id="partials/personFirstName.html">
    <span>{{first}}</span>
</script>

<script type="text/ng-template"    id="partials/personLastName.html">
    <span>{{last}}</span>
</script>

<script type="text/ng-template" id="partials/personWholeName.html">
    <div>
        <person-first-name first="name.first" />
        <person-last-name last="name.last" />
    </div>
</script>


<script type="text/ng-template" id="partials/person.html">
    <div>
        <person-whole-name name="person.name" />
    </div>
</script>

<div ng-controller="MainAppController">
    First Name: <input type="text" ng-model="person.name.first" /><br/>
    Last Name: <input type="text" ng-model="person.name.last" /><br/>
    <person person="person" />
</div>

然后是我的脚本:

'use strict;'

var mainApp = angular.module('mainApp', ['personModule'])
    .controller('MainAppController', function MainAppController($scope) {
        $scope.person = {
            name: {
                first: "John",
                last: "Smith"
            }
        };
    });

var personModule = angular.module('personModule', []);

personModule.directive('personFirstName', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'partials/personFirstName.html',
        scope: {
            first: '='
        }
    };
});

personModule.directive('personLastName', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'partials/personLastName.html',
        scope: {
            last: '='
        }
    };
});

personModule.directive('personWholeName', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'partials/personWholeName.html',
        scope: {
            name: '='
        }
    };
});

personModule.directive('person', function() {
    return {
        restrict: 'E',
        replace: true,
        templateUrl: 'partials/person.html',
        scope: {
            person: '='
        }
    };
});

关于为什么不呈现姓氏的任何想法?

4

2 回答 2

1

这是因为您试图在同一级别获取 2 个指令以使用不同的范围。例如,使用 aspan将它们分开可以解决问题

http://jsfiddle.net/S2cQD/6/

于 2013-06-08T13:17:09.350 回答
0

您的代码绝对没有问题。它的工作方式是:

工作小插曲

jsFiddle 可能有问题。

于 2014-06-05T05:23:37.530 回答