[英]How to reference the child scope from the parent controller?
以下代碼僅顯示一個表單,並使用editDownload
方法保存輸入:
JS:
$scope.editDownload = function(downloadId) {
var Download = Parse.Object.extend('Download')
var download = new Download()
var data = {
'title': $scope.download.title,
'link': $scope.download.link
}
download.save(data, {
success: function(result) {
console.log('Success:', result.toJSON())
},
error: function(result, error) {
alert('Error:', error.message)
}
})
}
HTML:
<ul class="text-center no-bullet">
<li ng-repeat="download in downloads">
<form>
<input type="text" class="form-control" ng-model="download.title">
<input type="text" class="form-control" ng-model="download.link">
<button type="button" class="btn btn-primary" ng-click="editDownload('{{download.objectId}}')">Submit</button>
</form>
</li>
</ul>
但是,有一個問題, $scope.download.title
和$scope.download
變得undefined
因為它們的值是在ng-repeat
因此位於不同的范圍內。
如何從父控制器引用此“子”作用域?
注意:我不能在模板中執行$parent.download.title
和$parent.download.link
,因為ng-model
將不顯示任何內容(它將不再引用ng-repeat中的子代。
您不必通過表達式將參數傳遞給editDownload()
。 您可以只傳遞editDownload(download.objectId)
。
結果按鈕標簽如下所示:
<button type="button" class="btn btn-primary" ng-click="editDownload(download.objectId)">Submit</button>
另外, $scope.download
也不會引用ng-repeat中的download
。
如果要對通過editDownload()
傳遞的download
對象進行某些操作,則可以僅將對象本身作為參數傳遞。
editDownload(download)
而不是將download.objectId
傳遞為參數。
最后,它應如下所示:
<button type="button" class="btn btn-primary" ng-click="editDownload(download)">Submit</button>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.