为什么当我再次单击按钮时未调用angularjs控制器? [英] Why angularjs controller is not called when i click the button again?

查看:40
本文介绍了为什么当我再次单击按钮时未调用angularjs控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的网页,左侧有按钮,右侧是ng-view的区域(在我的情况下,是几个复选框和提交"按钮)

Hi i have a webpage like this, left side has button, right side is the area for ng-view (in my case, several checkboxes and submit button)

当我单击按钮时,它将1.使用路由提供程序,它将到达其控制器和模板URL.2.控制器将从后端(node.js)查询一些信息3.模板URL将使用上面的信息来显示初始复选框选项.

When i click the button, it'll 1. using the route provider, it'll reach its controller and template URL. 2. The controller will query some info from back end side (node.js) 3. The info above will be used by template URL to display initial checkbox options.

现在,此过程第一次正常运行.但是,当我再次单击该按钮时,我希望它会再次调用其控制器,但是从调试器看来,什么都没发生,因此未调用控制器.

Now this procedure works fine for the 1st time. But when i click the button again, i was hoping it'll call its controller again, but from debugger, seems nothing happened, controller is not called.

非常困惑,为什么请这样???

So very confused, why is this please ???

在服务器端,

app.get('/2getMyDiagValue', function(req, res)
{
    console.log("get my diag");
    var v1=0, v2=0;
    var shellCmd = "... some executable ... ";

    exec(shellCmd, function(error, stdout, stderr) {
        if(error) {
            console.log("Error running getting sid");
        } else {
            // get v1 and v2 from stdout
        }

    res.json( {"mystuff1":v1, "mystuff2":v2} );

});

app.post('/2setMyDiagValue', function(req, res)
{
    // get the checkbox options from webpage,
    // and save them in the backend
}


在客户端,


in the client side,

app.config(['$routeProvider',
    function($routeProvider) {
         $routeProvider.when('/5MyDiag', {
             templateUrl: 'partials/MyDiag.html',
             controller: 'MyDiagController' 
         });
    }
]);

app.controller('myDiagController', function($scope, $http, $routeParams, QueryMyService) {

    // using http.get() to get existing my setting from server side
    QueryMyService.getInfoFromUrl7('/2getMyDiagValue').then(function(result) {
        $scope.formData = result.formDataObjects;
    }, function(error) {
        alert("Error");
    } );

    $scope.submitForm = function() {
        console.log("posting form data ...");
        $http.post("/2setMyDiagValue", 
                   JSON.stringify($scope.formData)).success(function(){} );
    };

});

app.factory('QueryMyService', function($http, $q, $location) {

    var factory = {};
        var browserProtocol = 'http';
        var address = 'localhost';
        var server = browserProtocol + '://' + address;

    //////////////////////////////////////////////////////////
     factory.getInfoFromUrl7 = function(myUrl) {
        var deferred = $q.defer();

        $http.get(myUrl).success(function(data) {
            deferred.resolve(data);
        }).error(function(){
            deferred.reject();
        }); 

      return deferred.promise;
    }

    return factory;
}


复选框网页本身:MyDiag.html


checkbox webpage itself: MyDiag.html

<form ng-submit="submitForm()" ng-controller="myDiagController">
    <div class="control-group" style="color:black">
        <label>My Checkbox</label>
        <div class="checkbox">
            <label class="checbox-inline" >
                <input class="big-checkbox" type="checkbox" ng-model="formData.myStuff1" 
                   ng-true-value="1" ng-false-value="0" ng-checked="formData.myStuff1 == 1">
                       <h4>Message 1</h4>
                <input class="big-checkbox" type="checkbox" ng-model="formData.myStuff2" 
                   ng-true-value="1" ng-false-value="0" ng-checked="formData.myStuff2 == 1">
                       <h4>Message 2</h4>
            </label>
        </div>

    <br>
    <input class="btn-primary" type="submit">

</form>


index.html


index.html

<div class="container">
    <a class="btn btn-md btn-info custom-btn" ng-href="#/5MyDiag">Diagnostics</a>
</div>

由于我需要删除变量中的公司名称,所以可能会出现不匹配的情况,但是想法是相同的.谢谢您的帮助.

Since i need to remove company names in the variable, there might be mismatch, but idea is the same. Thank you for your help.

推荐答案

如果您对angular的Web GUI没有任何改变,例如与大师联系,那应该是这样.就我而言,我两次单击了相同的按钮,第二次单击将不会再次调用路由提供程序.

Talked with guru, it's supposed to be so, if angular feels no change to the web GUI, e.g. in my case, i clicked the same button twice, it won't call the route provider again for the 2nd click.

如果您知道此概念,则无需阅读我的代码即可回答这个问题.

If you knew this concept, you don't need to read my code to answer this question.

希望我能拿回4分.

这篇关于为什么当我再次单击按钮时未调用angularjs控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆