2015-04-18 39 views
4

我構建了一個應用程序,顯示消息時,當用戶按下edit我想用它的值初始化消息textarea我創建了一個名爲notification的工廠以從我的數據中獲取消息基地和控制器我稱這種工廠:Angular:如何使用默認值初始化textarea

$scope.notification = notification.getNotification($stateParams.id); 
    $scope.notification.then(
    function(notification) { 
     $scope.notification = notification; 
     console.log($scope.notification.message); 
    }, 
    function(response) { 
     console.log('error fetching the notification', response); 
    } 
); 

和HTML:

<textarea ng-model="item.message" class="form-control" ng-init="item.message={{notification.message}}"></textarea> 

textarea的保持爲空,這就是我在我的元素檢查了:

<textarea ng-model="item.message" class="form-control ng-pristine ng-untouched ng-valid" ng-init="item.message=hello"></textarea> 
+0

'' – YOU

回答

0

ng-init在ng-repeat中有一個特殊用例,請參閱ng-init文檔以獲取有關它的用途的詳細信息。

您應該將item.message設置爲控制器中的某個值,該值處理視圖的此部分的作用域。基本設置:

$scope.item = {message:$scope.notification.message}; 

,或者$ scope.item已經是一個對象:

$scope.item.message = $scope.notification.message; 
+0

它工作時,我在工廠調用這樣做: $ scope.notification.then( 函數(通知){$ scope.item = {}; $ scope.item.message = notification.message; } ,但我仍然希望textarea的的新值把它發回服務器,因爲它是更新操作。對不起,我沒有弄清楚如何在評論中包裝代碼 –