2016-04-19 46 views
4

我已經做了一個簡單的自定義指令的例子來顯示一個人的名字。它仍然沒有顯示它。有人可以幫忙嗎?一個簡單的指令,顯示用戶名不工作

<html> 
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> 
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 
    <person></person> 
<script> 
    //module declaration 
    var app = angular.module('myApp',[]); 
    //controller declaration 
    app.controller('myCtrl',function(){ 
     $scope.name = "Peter"; 
    }); 
    //directive declaration 
    app.directive('person',function(){ 
     return { 
     restrict: 'E', 
     template: '<div>' {{ name }} '</div>' 
     }; 
    }); 
</script> 
</body> 
</html> 

回答

4

您只需要使用正確的Javascript語法。我在談論字符串連接(你不需要)。您還忘記將$scope注入控制器。正確的語法:

// module declaration 
 
var app = angular.module('myApp', []); 
 

 
// controller declaration 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.name = "Peter"; 
 
}); 
 

 
// directive declaration 
 
app.directive('person', function() { 
 
    return { 
 
    restrict: 'E', 
 
    template: '<div>{{name}}</div>' 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    <person></person> 
 
</div>

上面會爲你工作,但是這僅僅是簡單的語法的東西。更重要的是你不應該寫這樣的指令,只是因爲他們不增加真正的價值。您可以在模板中直接編寫{{name}},而無需指令。

現在有用指令會是這個樣子:

// module declaration 
 
var app = angular.module('myApp', []); 
 

 
// controller declaration 
 
app.controller('myCtrl', function($scope) { 
 
    $scope.user = { 
 
    name: "Peter", 
 
    age: 12, 
 
    hobby: "Coding, reading" 
 
    }; 
 
}); 
 

 
// directive declaration 
 
app.directive('person', function() { 
 
    return { 
 
    scope: { 
 
     data: '=' 
 
    }, 
 
    template: 
 
     '<div>{{data.name}}, {{data.age}}</div>' + 
 
     '<small>{{data.hobby}}</small>' 
 
    }; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    <person data="user"></person> 
 
</div>

在上面的例子中,你隔離指令的範圍(因此它成爲一個UI組件),使其真正可重用和靈活。喜歡這種方法。

+0

很不錯的交代! – Deadpool

3

兩個問題:

  • 你不控制器注入$範圍
  • 模板是錯誤的

工作代碼片段:

//1 Module declaration 
 
    var app = angular.module('myApp',[]); 
 
    //controller declaration 
 
    app.controller('myCtrl',function($scope){ 
 
     $scope.name = "Peter"; 
 
    }); 
 

 
//directive declaration 
 
    app.directive('person',function(){ 
 
     return { 
 
     restrict: 'E', 
 
     template: '<div>{{ name }}</div>' 
 
     }; 
 
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    <person>{{name}}</person> 
 
</div>

0

的index.html

<html> 
<head> 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.8/angular.min.js"></script> 
    <script src="app.js"></script> 
</head> 
<body ng-app="myApp" ng-controller="myCtrl"> 
    <person></person> 

</body> 
</html> 

app.js

//module declaration 
var app = angular.module('myApp',[]); 
    //controller declaration & inject $scope 
    app.controller('myCtrl',function($scope){ 
     $scope.name = "Peter"; 
    }); 

    //directive declaration 
    app.directive('person',function(){ 
    return { 
    restrict: 'E', 
     template: "<div> {{ name }}</div>" 
     }; 
    });