好的,我有这个HTML页面,其中包含一个select元素。该select元素将具有三个选择(请参阅下面这些选择的$ scope定义...:

<div id="mainID" ng-controller="theController">
    <form name="assetTypeForm" class="form-horizontal" role="form" novalidate>
        <select id="assetTypeSelect" name="asset.assetTypeList" style="width: 135px;"
            ng-model="asset.assetTypeList"
            ng-options="option.name for option in assetTypeListOptions track by option.value"
            ng-change="regularAssetTypeToggleClick(asset)"
            class="form-control" required>
            <!-- This is a HACK to trick Angular to make the first option value = 0 -->
            <option value="" style="display: none;">Choose Type</option>
        </select>
        <button type="button"
                ng-click="createAsset({{asset.assetTypeList}});" //This is the value I'm trying to pass...
                class="btn btn-primary btn-small">
                Create Asset
        </button>
    </form>
</div>


接下来,在我的控制器中,从按钮中调用此函数:

$scope.createAsset = function (assetType) { //As you can see with assetType (arg) I want to pass what the user selected in the dropdown select box.

        $scope.$broadcast('show-errors-check-validity');
        console.log("The asset I want to create is: " + assetType);

        if ($scope.assetTypeForm.$invalid) {

            console.log("BUMMER! The assetTypeForm is NOT valid: " + $scope.assetTypeForm.$invalid);
            $scope.submitted = false;
            return;

        } else {

            //Open the dialog for creating a graphic element
            $scope.openCreateElement(assetType);

        }

    };


我有assetType下拉列表的定义:

    $scope.assetTypeListOptions = [
        {
            name: 'Choose Type...',
            value: 'choose'

        }, {
            name: 'EULA',
            value: 'eula'

        }, {
            name: 'GRAPHIC',
            value: 'graphic'

        }];
    //This sets the default for the list
    $scope.assetTypeList = $scope.assetTypeListOptions[0];


我在console.log中得到的是:

The asset I want to create is: [object Object] <-- Here, is where either EULA, Graphic or Choose Type... where Choose Type... will throw an alert to tell the user, via show-errors{} that they NEED to select either EULA or Graphic.


而已....

谢谢

OK更新:作为回应,评论:

所以,你的意思是;我可以将您建议的内容传递给此函数:[[[$ scope.assetTypeListOptions [$ scope.asset.assetTypeList] .value]]]到函数中:“ createAsset({{asset.assetTypeList}});”像这样?

最佳答案

按命令:

  ng-click="createAsset({{asset.assetTypeList}});"


您无需在指令中使用{{}}

  ng-click="createAsset(asset.assetTypeList);"


但是您甚至不需要通过它,因为您选择的模型始终可以作为

$scope.asset.assetTypeList




另外你有错误的初始化:

$scope.assetTypeList = $scope.assetTypeListOptions[0];


您使用ng-model="asset.assetTypeList",因此应初始化:

$scope.asset.assetTypeList = $scope.assetTypeListOptions[0];


确保$scope.asset之前已初始化。

否则,请使用ng-model="assetTypeList"

09-30 21:12