不能设置财产的称呼'未定义的使用ng-model =" vm.client.salutation"

时间:2015-10-25 11:09:44

标签: javascript angularjs

我在为custom指令分配默认值时遇到了一些麻烦: 如果我这样做:vm.client= 'Dhr.';然后将ng模型更改为此ng-model="vm.client"它的工作原理。但我想如果我使用ng-model="vm.client.salutation"它不起作用。任何想法,为什么会发生这种情况?

<div ng-controller="AddClientController as vm">
    <form name="form_addClient" ng-submit="vm.add()">

    <div class="row">
        <!-- salutation -->
        <div class="col-md-4">

  <bootstrap-dropdown  ng-model="vm.client.salutation" data-placeholder="Aanhef" data-dropdown-data="servicesData.listOfServiceNames"></bootstrap-dropdown>

            </div>
        </div>

....

这是我的控制者:

(function () {
    'use strict';

    angular
        .module('app')
        .controller('AddClientController', AddClientController);

    AddClientController.$inject = ['ClientService', '$location', '$scope' ];
    function AddClientController(ClientService, $location, $scope) {

        var vm = this;

        vm.add = add;

        $scope.form = {};

        vm.client.salutation = 'Dhr.';

        $scope.servicesData = {
            listOfServiceNames : [
                "Mevrouw",
                "Mw.",
                "Dhr.",
                "Dr.",
                "Heer"
            ]
        }

        function add() {
            console.log("add Client")
            vm.dataLoading = true;
            ClientService.Create(vm.client)

        }
    }

})();

1 个答案:

答案 0 :(得分:6)

试试这个:

vm.client = {};
vm.client.salutation = 'Dhr.';