我應該給我的指令什麼範圍,以便輸入顯示初始值「TOTO」? 我不想拿範圍:真指令與輸入範圍破壞ngmodel
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 src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<input customattr type = "text" ng-model="value.name" />
</body>
</html>
JS代碼:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.value = {"name":"Toto", "id":1};
});
app.directive('customattr', function() {
return {
restrict: 'A',
scope: {
},
link: function (scope, element, attrs) {
}
};
});
Plunker這裏: http://plnkr.co/edit/JxWElWhTeBbNpFHS0wYT
好的。我知道customattr和ng-model有不同的範圍,但我想我可以通過在一個變量上使用範圍綁定「=」(我不知道哪一個)。綁定「=」不是調和兩個不同範圍的主要點? 重點是我希望範圍受到限制,以便我的指令更一般。 – JohnCastle 2013-04-22 11:35:32
@JohnCastle那些是兩個不同的問題。問題是,通過使用scope = {val:'='}語法,您正在爲此DOM元素創建一個新的,隔離的作用域,並遇到上述問題。 – 2013-04-22 11:39:09
@JohnCastle還有一件事 - 如果你能詳細說明你的真實用例,也許我可以提出一個解決方案。 – 2013-04-22 11:43:25