我在控制器中使用ui-select,我需要侦听控制器对ng模型的更改,这是我的HTML:
<div id="countryCtrl" country-selection class="form-inline">
<ui-select ng-model="selectedCountry" theme="selectize" style="{{$cat_style}}">
<ui-select-match placeholder="Select or search ...">
@{{$select.selected.title}}
</ui-select-match>
<ui-select-choices repeat="country in countries | filter: $select.search">
<span ng-bind-html="country.title | highlight: $select.search"></span>
</ui-select-choices>
</ui-select>
</div>
在国家/地区选择控制器:
angular.module('mainCtrl').directive('countrySelection', ['Country','state', function(Country, state) {
var linkF = function (scope, element, attrs, widgetPath) {
scope.$watch("selectedCountry", function (neww, old) {
console.log(scope.selectedCountry);
widgetPath.selectedCountry= widgetPath.model.selectedCountry;
scope.update("state.country.changed",scope.selectedCountry);//widgetPath.model.selectedCountry);
}, true);
};
return {
require: "^widgetPath",
restrict: 'A',
link: linkF,
scope: {}
}
}]);
如果我在 ui-select 指令中将国家/地区选择设置为如下所示的属性,手表将起作用:
<ui-select ng-model="selectedCountry" country-selection theme="selectize" style="{{$cat_style}}">
但是,然后,我将无法隔离country-selection
的范围,并且会出现错误
Multiple directives [countrySelection, uiSelect] asking for new/isolated scope on:
那么,我将如何从父指令country-selection
ui-select
指令中查看ng-Model
属性?
有一个 on-select 属性,从那里你可以在你的作用域上调用一个函数。
<ui-select ng-model="person.selected" theme="select2" on-select="someFunction($item, $model)" ...