-3

app.js でコントローラーとディレクティブを定義し、index.html で使用します。

app.js

var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
    $scope.name = 'World';
});
app.directive('search', function() {
    return {
        scope: {
            display: '='
        },
        restrict: 'AE',
        replace: true,
        link: function(scope, elm, attrs) {
            alert(display);
        }
    };
});

index.html

<!DOCTYPE html>
<html ng-app="plunker">
<head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>
        document.write('<base href="' + document.location + '" />');
    </script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.2.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.17/angular.min.js" data-semver="1.2.17"></script>
    <script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
    <search display="name"></search>
</body>
</html>

ディレクティブで alert(display) を指定すると、「world」と出力されます。

4

1 に答える 1

0

次のようになりますscope.display

    link: function(scope, elm, attrs) {
       alert(scope.display);
    }
于 2014-07-11T06:37:47.090 に答える