我有,看起來像如何訪問我的指令模板中的ng-model值?
<!-- selectList.tpl.html -->
<div ng-if="selectList">
<p>Id: {{$id}}</p>
<p>Current list item {{currentItem}}</p>
<select ng-model="currentItem"
name="current-item"
ng-options="item as item.value group by item.group for item in selectList">
<option value="">All</option>
</select>
</div>
我想從我向鏈路函數內部訪問currentItem
值來創建一個表函數,即模板的指令,
app.directive('selectList', [
"$rootScope",
"$timeout",
function (
$rootScope,
$timeout
) {
"use strict";
var getList = function() {
// ...
};
return {
restrict: 'E',
templateUrl: 'selectList.tpl.html',
link: function(scope, element, attrs) {
scope.selectList = getList();
scope.currentItem = "";
console.log("scope id:", scope.$id);
scope.$watch('currentItem', function(item) {
$timeout(function() {
console.log("currentItem is", item);
angular.element("#console").append("<p>Updated item: " + item + "</p>");
});
});
}
};
}
}
然而,在scopecope下創建一個子範圍,用於存儲對選擇框值的更改。如何訪問我的指令鏈接代碼中的選擇框更改?
我正在使用Angular 1.1.5。
這是問題的一個plunker(已在Q更新的代碼,以反映plunker):http://plnkr.co/edit/5eOaRE?p=preview
如果你有幾分鐘的時間,你可以設置一個plunkr嗎? – eddiec
很難說出你在什麼範圍內玩遊戲而沒有顯示指令代碼 – charlietfl
已添加plunker – zlog