[AngularJS] New in Angular 1.3 - bindToController

If you want to use controllers, instead of a link function, you can use bindToController.

 

<!DOCTYPE html>
<html ng-app="app">
<head>
    <title>Egghead.io Tutorials</title>
    <style>body{padding:20px}</style>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet">
    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.3/angular.js"></script>
    <script src="app.js"></script>
</head>
<body>

<note message="hello"></note>
</body>
</html>
angular.module("app", [])

    .directive("note", function note() {
        return {
            scope: {
                message: "@" //pass in a string
            },
            bindToController: true,
            controller: "NoteCtrl as note",
            template: "<div>{{note.message}}</div>"
        };
    })

    .controller("NoteCtrl", function NoteCtrl() {
        var note = this;
    });

 

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。