简体   繁体   English

如何更改函数中的angularjs元素值?

[英]How to change angularjs element value within a function?

HTML: HTML:

<div data-ng-app="story"  data-ng-init="detail='post a story here'">


<div data-ng-controller="detail_controller">
    <input type="text" name="detail" data-ng-model="detail">
    <h1>{{detail}}</h1>
    <input data-ng-click="submit_detail(detail)" type="submit" name="submit_detail" value="Post detail">


    <!--detail dashboard-->
    <div ng-model = "detail_dashboard" data-ng-show = "detail_dashboard.show">

        <div><h1 data-ng-model = "title" class="title">detail: {{title}}</h1></div>
        <div data-posted-answers></div>
    </div>
</div>

</div>

Angular js 角js

var app = angular.module('story', []);


function updateArticle(data) {
    console.log('updateArticle called '+data);

how to change the value here? 如何在这里更改值?

    $rootScope.title = 'hidfg';
}

app.controller('detail_controller', function($scope) {


    $scope.detail_dashboard = {
        show : false
    };

    $scope.submit_detail = function($value) {

        $scope.detail_dashboard = {
            show : true
        };

        $scope.title = $value;

        updateArticle($value);

    };



});

$scope and $rootScope give this error when used inside updateArticle function: 当在updateArticle函数内部使用时,$ scope和$rootScope会出现此错误:

ReferenceError: `$rootScope` is not defined

How to change any element value inside a controller? 如何更改控制器内的任何元素值? In jQuery, simply call the element by name, id, class and data attribute. 在jQuery中,只需按名称,id,类和数据属性调用元素。 But angular how can I do that? 但是我该怎么做呢?

I tried this way as well: 我也尝试过这种方式:

angular.element('title').text(''something new);

But that gave me jqLite:nosel error 但这给了我jqLite:nosel错误

Fiddle: https://jsfiddle.net/hadfgy4r/ 小提琴: https//jsfiddle.net/hadfgy4r/

This is the answer you're looking for: https://jsfiddle.net/27pawemr/2/ 这是您要寻找的答案: https : //jsfiddle.net/27pawemr/2/

       var app = angular.module("story", []);
     //You can't do this, because you're out of the "Angular consciousness"
      /*function updateDashboard(data) {
          console.log('updateArticle called '+data);
           $rootScope.title = 'hidfg';
       }*/

       app.service("myModifier", ["$rootScope", function($rootScope) {
            this.updateDashboard = function(data) {
               console.log('updateArticle called ' + data);
              $rootScope.title = 'hidfg';
           };
    }]);

     app.controller("detailController", ["$scope", "myModifier", function($scope, myModifier) {
     $scope.detail = "post a story here";

     $scope.detailDashboard = {
        show: false
    };

   $scope.submit_detail = function(value) {
      //There's no need to redeclare $scope.detailDashboard
      $scope.detailDashboard.show = true;

      $scope.title = value;

       myModifier.updateArticle(value);

  };



}]);

Also there's an error in your HTML. HTML中也有错误。 The ng-model works only for inputs! ng模型仅适用于输入!

<div><h1 class="title">detail: {{title}}</h1></div>
    <div data-posted-answers></div>
</div>

So it doesn't make any sense to put an ng-model here 所以在这里放一个ng模型没有任何意义

<div><h1 class="title">detail: {{title}}</h1></div>

it seems like scope() and $apply needed to be used like this. 似乎需要像这样使用scope()和$ apply。 But I need to include jquery for this. 但是我需要为此添加jQuery。 If below code can be modified without jquery, do let me know. 如果可以在不使用jquery的情况下修改以下代码,请告诉我。

function updateArticle(data) {
var scope = angular.element($("#pquestion")).scope();
    scope.$apply(function(){

        scope.detail_dashboard= {
            show : true
        };

        scope.title= data;
    });
}

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

相关问题 如何在不绑定angularjs的情况下更改元素的值? - How to change value of element without binding in angularjs? AngularJS:如何在指令中将函数动态添加到附加的html元素 - AngularJS: How to dynamically add a function to a appended html element within a directive 如何在javascript函数中更改Bool值? - How to change Bool value within a function in javascript? 如何在angularjs中设置值时检测输入元素的变化 - How to detect input element change when set value in angularjs AngularJs如何根据监视值更改回调函数 - AngularJs How to change CallBack Function based on Watch Value 更改angularjs指令链接函数中的元素类 - Change element class in angularjs directive link function AngularJS-如何更改包含数据绑定的模板中的元素? - AngularJS - How do I change an element within a template that contains a data-binding? 如何从自己的更改函数中更改下拉框的值,以便再次调用更改函数 - How to change the value of a dropdown box from within it's own change function so that the change function is called again 如何更改函数中嵌入的变量的值? - How can I change the value of a variable embedded within a function? Angularjs在具有验证的同一元素的指令内更改ng-model - Angularjs change ng-model within a directive on the same element with validations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM