-3

我在 app.js 中定义了一个控制器和指令,并在 index.html 中使用它。

应用程序.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);
        }
    };
});

索引.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 回答