无法从指令
问题描述:
获得价值回我有这个指令无法从指令
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;
});
}
}
});
我的模板
<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它。
此处,我用我的指示
<country-select selected-value="custCtrl.editForm.country"></country-select>
和控制台登录变量custCtrl.editForm.country只是给出不确定的HTML。
我以为“=”是双向数据绑定? 我错过了什么?
谢谢
答
ng-options
语法错误。该语法中的第一项表示选项值,因此是ng-model
将镜像的值。你已经指定了一个名为name
的变量,事实上这个变量是未定义的。它应该是country.name
:
ng-options="country.name as country.name for country in countries"
(第二届country.name
指定选项标签,即在下拉列表中显示的文本这并不需要搭配第一项,但经常会做的。)
DEMO:Here is a fiddle you can play around with
在这里面,你会看到有哪个日志从控制器和指令每当模型更改时都控制台手表。控制器还在HTML中显示选定的值。 (我向手表的控制器添加了$scope
,但生产时不需要它。)
您传递给指令的变量必须是一个对象。你有没有确定是这样? –