当由子控制器修改时,Angularjs模型不会更新视图

时间:2016-01-13 15:48:42

标签: angularjs

我必须从嵌套控制器更新父控制器的参数。 我能够读取参数,但是当我更改它时它不会更新到视图(网页)...帮助plz:)

这是我的js:

<div ng-controller="signalCtrl as signal">
    <input type="text" ng-model="signal.address">
    [...]
    <div ng-controller="reportMap as map">
    [...]
    </div>
</div>

这是我的HTML:

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  //declare named constants and variables
  const double RATE1 = .02;
  const double RATE2 = .05;
  double sales       = 0.0;
  double commission  = 0.0;

  //get input item
  cout << "Enter the sales amount: "; 
  cin >> sales; 

  //calculate commission
  if (sales <= 15000.0)
{
    commission = sales * RATE1;
else
    commission = sales * RATE2;

 }
//end if

//display commission
  cout << fixed << setprecision(2);
  cout << "Commission: $" << commission << endl;

system("pause");
return 0;
}   //end of main function

3 个答案:

答案 0 :(得分:0)

父控制器中的address属性未绑定到$scope,而是绑定到this对象,因此您甚至无法通过该方式访问该属性。我建议你将更新功能移到父控制器上:

app.controller('signalCtrl', [ '$scope', 'DB', function($scope, service) {
    var self = this;
    self.address = null;
    self.update = function(newValue) {
        self.address = newValue;
    }
}]);

app.controller('reportMap', ['$scope', function($scope) {
    var self = this;
    self.someValue = 'something';
}]);

HTML:

<div ng-controller="signalCtrl as signal">
    <input type="text" ng-model="signal.address">
    [...]
    <div ng-controller="reportMap as map">
        <button type="button" ng-click="signal.update(map.someValue)">Click!</button>
    [...]
    </div>
</div>

您不会在显示的代码中调用更新功能,因此我添加了一个按钮来说明如何使用它。

答案 1 :(得分:0)

使用信号代替标签

app.controller('reportMap', ['$scope', function($scope) {
    this.updateParent = function() {
        $scope.$parent.signal.address = 'something';
    }
}]);

请参阅此链接:https://plnkr.co/edit/RhQLfBy2heecJDuCcq9s?p=preview

答案 2 :(得分:0)

好的,可能错误在于HTML:

Content-Length

我从孩子那里调用setIndirizzo(我不传递我知道的值,但这不是问题):进入控制台我读到“AGGIORNAMENTO INDIRIZZO”,但是进入VIEW的值不会改变...

这是完整html文件的链接。 https://www.dropbox.com/s/gzlm997ub8fot7r/html.txt?dl=0