【问题标题】:How to watch nb-Model at child?如何在小孩看nb-Model?
【发布时间】:2015-04-13 22:30:40
【问题描述】:

我在控制器中使用 ui-select,我需要从控制器监听 ng-model 的更改,这是我的 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>

在 countrySelection 控制器中:

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 属性?

【问题讨论】:

    标签: angularjs angularjs-directive angularjs-scope ui-select


    【解决方案1】:

    有一个on-select 属性,您可以从那里调用您作用域上的函数。

    <ui-select ng-model="person.selected" theme="select2" on-select="someFunction($item, $model)" ...
    

    【讨论】:

    • 谢谢,那这样我就不用看ng-model的变化了?
    • 我注意到您提到的示例使用控制器作为 app.controller('DemoCtrl', .. 我需要隔离范围,这可以不使用链接吗?
    • 是的,我认为您可以对手表进行预判,并且可以有一个孤立的范围。如果方法来自控制器,您可以将其传递给具有范围的指令:{ methodName: '&'} 并在指令上使用它。
    猜你喜欢
    • 2020-08-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 2015-04-02
    • 2013-01-15
    相关资源
    最近更新 更多