【问题标题】:Send data to controller on ui.select angular将数据发送到 ui.select angular 上的控制器
【发布时间】:2014-08-15 11:20:53
【问题描述】:

我正在尝试从下拉列表中选择一个值并从 angularJs 中对应的选择 id 中获取数据

我想出了angular-ui/ui-select 指令

<ui-select ng-model="customer" theme="selectize">
     <ui-select-match placeholder="Customer List">{{$select.selected.Name}}</ui-select-match>
        <ui-select-choices repeat="customer in customers | filter: $select.search">
            <span ng-bind-html="customer.Name | highlight: $select.search"></span>
            <small ng-bind-html="customer.Id.toString() | highlight: $select.search"></small>
        </ui-select-choices>
</ui-select>

在我的控制器上我有获取客户方法

$scope.setActiveCustomer = function(customer) {
  customersService.getCustomers(customer).then(function(response) {
    $scope.selectedCustomer = response;
  });
};

我的问题是

  • 一旦我选择了客户我应该使用哪个事件来触发火灾 setActiveCustomer (比如 onchange 什么的)。当我使用ng-click 事件触发了很多次,因为ng-click 用于控制激活。

  • 另一件事是如何为ui-select 设置初始值?

谢谢

【问题讨论】:

    标签: angularjs angularjs-directive ui-select2


    【解决方案1】:

    根据ui-select FAQ,您的ng-model 表达式中应该有一个.,例如:

    <ui-select ng-model="model.customer" theme="selectize">
    

    要设置初始值,你可以设置ng-model中指定的属性,所以在你的控制器中:

    $scope.model = {
      customer: $scope.customers[0] // set the initial selected option
    };
    

    对于 onchange 事件,您可以像这样使用$scope.$watch()

    $scope.$watch('model.customer', function (customer) {
      $scope.setActiveCustomer(customer);
    });
    

    希望这会有所帮助。

    【讨论】:

    • 嗨 runTarmit 惊人地解决了我的第一个问题,但我仍然坚持第二个问题
    • 你的意思是设置一个初始值吗?
    • 是的。初始值未按预期分配!!
    • 能否请您展示如何使用设置$scope.model.customer$scope.customers 那个时候可用吗?
    • 是的,它是可用的,但是当它绑定到选择 ui 时,它只显示 [object Object] 我已经使用 firebug 检查了这些对象,它显示初始绑定对象是 [Object { Id=1, Name="FirstOne"}] 但它应该类似于 Object { Id=1511, Name="FirstOne", $$hashKey="00H"}
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 2017-03-11
    • 2016-10-04
    • 2018-01-18
    • 2013-09-23
    相关资源
    最近更新 更多