简体   繁体   English

角度值未从函数反映出来

[英]angular value is not reflecting from the function

I am trying to assign value to $scope variable from a controller function. 我正在尝试从控制器函数为$ scope变量赋值。 I can see the value is assigned from console but it is not reflecting in html page. 我可以看到该值是从控制台分配的,但未反映在html页面中。

my html 我的HTML

<div id="NodeDetails" class="NodeDetails">{{NodeDetails}}</div>

my controller function 我的控制器功能

$scope.ShowDetails = function() {
    $scope.NodeDetails = "in am called in function";
    console.log($scope.NodeDetails)     
};

i can see the data in console but same is not displayed in html. 我可以在控制台中看到数据,但是html中不会显示相同的数据。

it works when i put, 我放的时候有效

$scope.NodeDetails = "in am called in individual";

is there any specific way to set variable in function. 有什么具体的方法可以在函数中设置变量。 I have tried by return value also but that also didn't work. 我也尝试过用返回值,但这也没有用。

$scope.NodeDetails = function() {
return "in am called in function";  
};

please suggest. 请提出建议。

It should work either way, here's an example of different type of setters 无论哪种方式都可以工作,这是不同类型的二传手的例子

 angular.module("app", []).controller("myCtrl", function($scope){ $scope.val = "default value"; $scope.change = function(){ $scope.val = $scope.text; $scope.text =""; }; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="myCtrl"> {{val}} <input type="text" ng-model="text" placeholder="Add something"/> <button ng-click="change()">Change</button> </div> 

Instead of creating NodeDetails as a string, change it to an object and try changing it https://jsfiddle.net/paka2/d0mf0g9j/1/ 与其将NodeDetails创建为字符串,不如将其更改为对象并尝试将其更改为https://jsfiddle.net/paka2/d0mf0g9j/1/

<div id="NodeDetails" class="NodeDetails">{{nodeObject.NodeDetails}}</div>   
$scope.nodeObject = {
NodeDetails: ""
}

This solves the problem of any child scope issue 这解决了任何子范围问题

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

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