【问题标题】:Angular: how add event change on select (jQuery-UI combobox select)?Angular:如何在选择上添加事件更改(jQuery-UI 组合框选择)?
【发布时间】:2015-10-20 20:50:18
【问题描述】:

HTML:

<select style='width:60px;' combobox>
     <option ng-repeat='item in sizePriceWeighArr' value='{{ item.id }}' price='{{ item.price }}' weight='{{ item.weight }}' size='{{ item.size }}'>{{ item.size }}</option>
</select>

JS:

.controller('productController', ['$scope', '$location', '$http', '$q', '$window', '$stateParams', function($scope, $location, $http, $q, $window, $stateParams) {
    // Some code here to form array of products
    $scope.sizePriceWeighArr = productObj.sizePriceWeigh;
}])
.directive('combobox', function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            $(element).combobox();                              
        }
    }
})

在指令中,我形成了一个 jQuery-UI 组合框选择。但我需要以某种方式在选择、控制器或指令中捕获更改事件。

【问题讨论】:

    标签: javascript jquery angularjs jquery-ui


    【解决方案1】:

    你目前正在使用一个选择,所以为什么不直接使用 ngChange:

    <select style='width:60px;' combobox ng-change="changeFunction()">
    <option ng-repeat='item in sizePriceWeighArr' value='{{ item.id }}' 
    price='{{ item.price }}' weight='{{ item.weight }}' size='{{ item.size }}'>    
    {{ item.size }}</option>
    </select>
    

    js:

    .controller('productController', ['$scope', '$location', '$http', '$q', '$window', '$stateParams', function($scope, $location, $http, $q, $window, $stateParams) {
    // Some code here to form array of products
    $scope.sizePriceWeighArr = productObj.sizePriceWeigh;
    
    function changeFunction(){
        //your code.
    }
    }])
    .directive('combobox', function() {
    return {
        restrict: 'A',
        link: function($scope, element, attrs) {
            $(element).combobox();                              
        }
    }
    })
    

    【讨论】:

    • 错误:[$compile:ctreq] errors.angularjs.org/1.4.6/$compile/…
    • 问题是:如果我添加 ng-change + ng-model 它不起作用,因为 jquery-ui 组合框插件生成新的 html 并设置为选择显示:无样式。如果您希望浏览器上的元素并删除 display:none 样式,您将在页面上看到选择。当您尝试使用生成 jquery-ui 组合框插件的 html 更改值时,您会看到本机选择值也在发生变化,但 ng-change 函数不执行。
    猜你喜欢
    • 2016-09-28
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多