2014-08-30 32 views
0

我有四個不同的輸入,並且想要將更改的輸入的值發送到控制器,而無需按發送按鈕或其他。將ng-model的值發送到角度控制器

我如何得到它所有的輸入應該有下一個句法。

<input ng-model="borderRadius" ng-change="change()"> 
<input ng-model="background" ng-change="change()"> 

還是不是?

我想他們在我的app.js

control.controller('generatorOptions', function ($scope) { 

    $scope.buttonStyle = { 
    "border-radius" : " -- here is value of it -- ", 
    "background" : " -- here is value of it -- " 
    }; 

}); 

更新:這是工作得很好,但如何優化代碼? https://github.com/tanotify/Button-style-generator/blob/master/public/assets/scripts.min.js

+1

'ng-model'已經創建了scope屬性,只是給每個'ng-model'分配了按鈕屬性。 – charlietfl 2014-08-30 21:13:20

+0

這樣做是否正確? https://github.com/tanotify/Button-style-generator/blob/master/public/assets/scripts.min.js – Antoni 2014-08-30 21:56:49

+0

這應該是原樣。你提供的鏈接......如果你在解釋每個代碼塊的預期內容時會更好......否則很難給出相關評論 – harishr 2014-08-31 05:39:16

回答

0

首先,你需要在你的控制器來定義:

control.controller('generatorOptions', function ($scope) { 
    $scope.buttonStyle={}; 


    $scope.change = function() { 
    console.log("borderRadius value:"+$scope.buttonStyle.borderRadius); 
    console.log("background value:"+$scope.buttonStyle.background); 
    }; 

}); 

這是你的看法:

<input ng-model="buttonStyle.borderRadius" ng-change="change()"> 
<input ng-model="buttonStyle.background" ng-change="change()"> 

這意味着,首先你需要在你控制器定義對象(按鈕樣式),比這個對象在你的視圖中是可訪問的,並且你可以定義你可以在控制器中通過對象訪問的對象的新屬性(半徑和背景)。