隨着指令很簡單:
<!DOCTYPE html>
<html data-ng-app="app">
<head>
<title>Upload</title>
</head>
<body>
<div id="putControllerHereWithoutDirective" data-mytest>
{{test}}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script>
'use strict';
angular.module('app',[])
.directive("mytest", function() {
return {
restrict: 'A',
controller: function ($scope) {
$scope.test = 'mytest';
},
link: function (scope, el, attrs) {
function test() {
alert('Clicked');
}
el.on('click', test);
}
};
});
</script>
</body>
</html>
與綁定(酷:))
<!DOCTYPE html>
<html data-ng-app="app">
<head>
<title>Upload</title>
</head>
<body>
<div id="putControllerHereWithoutDirective">
{{test}}
</div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.min.js"></script>
<script>
'use strict';
angular.module('app',[])
.run(function($rootScope) {
var returnFn = angular.bind($('#putControllerHereWithoutDirective'),function(){
this.attr('data-ng-controller','myCtrl');
}, []);
returnFn();
})
.controller('myCtrl',function($scope){
$scope.test = 'My test';
})
</script>
</body>
</html>
我不知道它是否適合yourneeds但它是我認爲無鍋的第一件事:) http://docs.angularjs.org/api/angular.bind關於第二個想法,如果你可以把手放在標記上可能是一個指示是要走的路 – Whisher
@Whisher我會怎麼做呢?你可以使用'bind'和''directive'來編寫一些示例代碼嗎? –