Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法从指令中获取值_Javascript_Html_Angularjs_Angularjs Directive_Ng Options - Fatal编程技术网

Javascript 无法从指令中获取值

Javascript 无法从指令中获取值,javascript,html,angularjs,angularjs-directive,ng-options,Javascript,Html,Angularjs,Angularjs Directive,Ng Options,我有这个指令 app.directive('countrySelect', function (Country) { return { templateUrl: 'template/directives/countryselect.html', restrict: 'E', scope: { selectedValue: '=' }, link: function (scope) { Country.success(func

我有这个指令

app.directive('countrySelect', function (Country) {
return {
    templateUrl: 'template/directives/countryselect.html',
    restrict: 'E',
    scope: {
        selectedValue: '='
    },
    link: function (scope) {
        Country.success(function (data) {
            scope.countries = data;
        });
    }
}
});
<country-select selected-value="custCtrl.editForm.country"></country-select>
我的模板

<select chosen
    data-placeholder="Country"
    search-contains="true"
    ng-model="selectedValue"
    ng-options="name as country.name for country in countries">
<option value=""></option>
</select>

我已将模型设置为selectedValue,但只有在尝试console.log时才返回undefined

这是我使用指令的HTML

app.directive('countrySelect', function (Country) {
return {
    templateUrl: 'template/directives/countryselect.html',
    restrict: 'E',
    scope: {
        selectedValue: '='
    },
    link: function (scope) {
        Country.success(function (data) {
            scope.countries = data;
        });
    }
}
});
<country-select selected-value="custCtrl.editForm.country"></country-select>

控制台记录变量custCtrl.editForm.country只会给出未定义的结果

我以为“=”是双向数据绑定? 我错过了什么


谢谢

ng选项语法错误。该语法中的第一项表示选项值,因此是
ng model
将镜像的值。您指定了一个名为
name
的变量,该变量实际上未定义。它应该是
country.name

ng-options="country.name as country.name for country in countries"
(第二个
country.name
指定选项标签,即下拉列表中显示的文本。这不需要与第一个术语匹配,但通常会匹配。)


演示:


在其中,您将看到,每当模型发生更改时,都有一些监视程序从控制器和指令记录到控制台。控制器还将在HTML中显示选定的值。(我为手表的控制器添加了
$scope
,但在生产中不需要它。)

传递给指令的变量必须是对象。你确定是这样吗?