Angularjs范围内的函数数据

时间:2017-11-29 06:58:22

标签: angularjs

我正在尝试将数据显示在我的视图中,但$scope.plan输出{}。我想它会从initGetEdit输出预期数据中的console.log() 函数$http.post输出获取的数据。< / p>

controller.js

var id = $stateParams.id;

$scope.plan = {}

$scope.initGetEdit = function(){
    var data = { id : id }
    $http.post("someUrl", data).then(function(res){
        $scope.plan = res.data;
        console.log($scope.plan); //----> logs expected data
    })
}
$scope.initGetEdit();

console.log($scope.plan);  //----> logs {}

在我的view我有类似的东西。

查看

<input ng-model="plan.something" type="text" />

更新

首先感谢您提供的答案和评论,感谢您。它给了我一个见解。我通过移除initGetEdit 功能并仅使用http.post解决了我的问题。

2 个答案:

答案 0 :(得分:2)

尝试保持第二个控制台的监视。

$scope.$watch('plan',function(){
  console.log($scope.plan);
});

答案 1 :(得分:1)

首先,在$scope.plan = {}函数http调用$scope.initGetEdit函数之后,在函数http是异步调用后,您的对象可能会声明一个变量 var app = angular.module('testApp',[]); app.controller('testCtrl',function($scope,$http){ //var id = $stateParams.id; var id=1; $scope.plan = {} $scope.initGetEdit = function(){ var data = { id : id } //$http.post("http://someurl", data).then(function(res){ $scope.plan ={id:1,something:"hai this is response data"}; console.log($scope.plan); //----> logs expected data //}) } $scope.initGetEdit(); console.log($scope.plan); //----> logs {} });根据回复填写。直到它将是一个空物体。

@Ujjwala Bollam提到在控制台中打印它。

&#13;
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <div ng-app="testApp" ng-controller="testCtrl">
    
    <input ng-model="plan.something" type="text" />
    </div>
&#13;
df_new = pd.read_json('{"TA":{"229":-30.0,"230":-30.0,"192":23.0,"193":23.0,"248":60.0,"249":60.0,"126":-30.0,"127":-30.0,"88":23.0,"89":23.0,"150":60.0,"151":60.0,"239":-30.0,"240":-30.0,"197":23.0,"198":23.0,"256":60.0,"257":60.0,"135":-30.0,"136":-30.0,"94":23.0,"95":23.0,"164":60.0,"165":60.0,"438":-30.0,"439":-30.0,"291":23.0,"405":23.0,"453":60.0,"454":60.0,"341":-30.0,"342":-30.0,"292":23.0,"293":23.0,"365":60.0,"366":60.0,"445":-30.0,"446":-30.0,"410":23.0,"411":23.0,"462":60.0,"463":60.0,"357":-30.0,"358":-30.0,"297":23.0,"298":23.0,"371":60.0,"372":60.0},"Type":{"229":"A","230":"A","192":"A","193":"A","248":"A","249":"A","126":"P","127":"P","88":"P","89":"P","150":"P","151":"P","239":"A","240":"A","197":"A","198":"A","256":"A","257":"A","135":"P","136":"P","94":"P","95":"P","164":"P","165":"P","438":"A","439":"A","291":"A","405":"A","453":"A","454":"A","341":"P","342":"P","292":"P","293":"P","365":"P","366":"P","445":"A","446":"A","410":"A","411":"A","462":"A","463":"A","357":"P","358":"P","297":"P","298":"P","371":"P","372":"P"},"Value":{"229":57.36232,"230":52.97104,"192":59.82472,"193":56.70568,"248":72.30088,"249":68.56624,"126":71.68528,"127":79.15456,"88":84.1204,"89":82.2736,"150":77.26672,"151":81.00136,"239":70.41304,"240":82.2736,"197":76.03552,"198":83.5048,"256":82.8892,"257":88.51168,"135":89.74288,"136":97.21216,"94":99.1,"95":95.98096,"164":95.98096,"165":96.59656,"438":64.8316,"439":73.53208,"291":107.18488,"405":82.2736,"453":77.26672,"454":86.00824,"341":105.29704,"342":97.21216,"292":108.41608,"293":100.3312,"365":84.77704,"366":88.51168,"445":46.11736,"446":52.35544,"410":62.32816,"411":65.4472,"462":71.06968,"463":74.80432,"357":77.92336,"358":79.15456,"297":94.09312,"298":87.23944,"371":82.2736,"372":98.4844},"Group":{"229":"FA","230":"FA","192":"FA","193":"FA","248":"FA","249":"FA","126":"FA","127":"FA","88":"FA","89":"FA","150":"FA","151":"FA","239":"FB","240":"FB","197":"FB","198":"FB","256":"FB","257":"FB","135":"FB","136":"FB","94":"FB","95":"FB","164":"FB","165":"FB","438":"RB","439":"RB","291":"RB","405":"RB","453":"RB","454":"RB","341":"RB","342":"RB","292":"RB","293":"RB","365":"RB","366":"RB","445":"RC","446":"RC","410":"RC","411":"RC","462":"RC","463":"RC","357":"RC","358":"RC","297":"RC","298":"RC","371":"RC","372":"RC"}}')
g = sns.factorplot(x="Value", y="Type", hue="TA",
                   col="Group", data=df_new, col_wrap=2,
                   kind="strip", dodge=True, jitter=True, alpha=.5)
g = g.map_dataframe(sns.pointplot, x="Value", y="Type", hue="TA",
                    dodge=.532, join=False, palette="dark", markers="d", scale=.75, ci=None)

def myplot(x, y, **kwargs):
    ax = plt.gca()
    data = kwargs.pop("data")
    print(data.shape, "in plotting group", data.iloc[0]['Group'])
    groups = data.groupby([y, 'TA'])
    for label, group_df in groups:
        print("Group label:", label, "Group mean: {:.2f}".format(group_df[x].mean()))

g = g.map_dataframe(myplot, x="Value", y="Type")
g.set_titles(row_template="{row_name}", col_template="{col_name}")
&#13;
&#13;
&#13;