无法使用ng-model ="vm.client.salutation"设置未定义的属性"称呼"

Gre*_*reg 1 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>

....
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

(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)

        }
    }

})();
Run Code Online (Sandbox Code Playgroud)

Maj*_*uti 6

试试这个:

vm.client = {};
vm.client.salutation = 'Dhr.';
Run Code Online (Sandbox Code Playgroud)