简体   繁体   English

角度数据-将更新值绑定到范围时遇到麻烦(使用滑块)

[英]Angular data - having trouble binding updated values to scope (using slider)

I'm trying to use Angular to create a tool that accepts user input from a slider tool, and automatically updates an "estimate" field whenever the values are changed. 我正在尝试使用Angular创建一个工具,该工具接受来自滑块工具的用户输入,并在更改值时自动更新“估计”字段。 However, the data only seems to be binding on the way to the view; 但是,数据似乎仅在视图的途中具有约束力。 the user input is displayed, but doesn't seem to register in the scope. 显示用户输入,但似乎未在范围中注册。

Here's the HTML: 这是HTML:

<h3 ng-controller="calcController" ng-model="estimate" class="floating-menu">${{estimate}}</h3>
    <form ng-controller="calcController" method="post" action="/estimate">

        <div class="container">
            <div class="vals">${{values.insurance}}</div>
            <div id="insurance-slider">
                <slider floor="0" ceiling="250" value="125" step="25" precision="2" ng-model="values.insurance" ng-change="changeVal()" translate="currencyFormatting"></slider>
            </div>

        <div class="container">
            <div class="vals">${{values.lease}}</div>
            <div id="lease-slider">
                <slider floor="0" ceiling="250" value="125" step="25" precision="2" ng-model="values.lease" translate="currencyFormatting"></slider>
            </div>
        </div>
     </form>

Here's the Angular: 这是角度:

 //create the module
   angular.module('Breeze', ['ngRoute', 'ngResource', 'ui.slider'])

   .config(function($routeProvider) {
       $routeProvider

       // route for the calculator page
           .when('/', {
           templateUrl: 'partials/calculator.html',
           controller: 'calcController'
       });
   })

    // create the controller and inject Angular's $scope
   .controller('calcController', function($scope, $http) {
       $scope.values = {
           // Default values
           insurance: 125,
           lease: 125,
       };

       $scope.estimate = $scope.values.insurance + $scope.values.lease
      }

       // Formatting for scale bar
       $scope.currencyFormatting = function(value) {
           return value.toString() + " $";
       }
   })

I've tried adding a $watch and a $broadcast function but can't seem to get them working. 我尝试添加$ watch和$ broadcast函数,但似乎无法使其正常工作。

This is what it looked like in Angular: 这是在Angular中的样子:

   $scope.$watch("values", function(newVal, oldVal, scope) {
       scope.estimate = addVals();
    })
  $scope.changeVal = function() {
    $scope.estimate = addVals();
    console.log('hi')

   $scope.estimate = $scope.values.insurance + $scope.values.lease
  }

Ideas? 想法?

Just setting them equal in the beginning wont work. 刚开始时将它们设置为相等是行不通的。 In your view, instead of having an estimate field just use this 在您看来,与其使用“估计”字段,不如使用它

<div ng-controller="calcController" ng-app="Breeze">
    <h3 ng-model="estimate" class="floating-menu">${{values.insurance + values.lease }}</h3>
        <form method="post" action="/estimate">

            <div class="container">
                <div class="vals">${{values.insurance}}</div>
                <div id="insurance-slider">
                    <slider floor="0" ceiling="250" value="125" step="25" precision="2" ng-model="values.insurance" ng-change="changeVal()" translate="currencyFormatting"></slider>
                </div>

            <div class="container">
                <div class="vals">${{values.lease}}</div>
                <div id="lease-slider">
                    <slider floor="0" ceiling="250" value="125" step="25" precision="2" ng-model="values.lease" translate="currencyFormatting"></slider>
                </div>
            </div>
           </div>
         </form>
</div>

This will keep them adding as the values change. 这将使它们随着值的变化而增加。

Another thing that would be breaking is that you have two controller calls to the same controller and no app. 另一件事可能会中断:您有两个控制器调用相同的控制器,而没有应用程序。 Need to change it as below with the new div and remove the rest of the ng-controllers. 需要使用新的div进行如下更改,并删除其余的ng-controllers。

Also, make sure that you have the ui slide javascript file included on your page. 另外,请确保页面上包含ui幻灯片javascript文件。

You are using two nested ng-controller="calcController" in your HTML. 您在HTML中使用了两个嵌套的ng-controller="calcController" Hence two instanciations of the controller (two $scope) which will have their own estimate value, so when you update the inner estimate, the upper one is not aware of that. 因此,控制器的两个实例(两个$ scope)将具有其自己的estimate值,因此,当您更新内部估计时,上层实例不会意识到这一点。

You should remove the second ng-controller in your <form> (if the controller were different, then you would put the estimate variable in an object such as $scope.model.estimate so properties inheritance between the two controllers actually work. But in your case there is no reason to have two controllers). 您应该在<form>删除第二个ng-controller(如果控制器不同,则可以将估算变量放在$ scope.model.estimate之类的对象中,以便两个控制器之间的属性继承真正起作用。您的案例中没有理由拥有两个控制器)。

Also, you should remove that ng-model="estimate" in your <h3> tag, which has no effect. 另外,您应该在<h3>标记中删除该ng-model="estimate" ,这无效。

Did you try to bind to object instead of primitive type? 您是否尝试绑定到对象而不是原始类型?

$scope.values.estimate = $scope.values.insurance + $scope.values.lease

see similar question here: Angularjs: 2 way binding not working in included template 在这里看到类似的问题: Angularjs:2方式绑定在包含的模板中不起作用

Here is a fiddle http://jsfiddle.net/U3pVM/9330/ 这是一个小提琴http://jsfiddle.net/U3pVM/9330/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM