Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
543 views
in Technique[技术] by (71.8m points)

Why angularjs controller is not called when i click the button again?

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)

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 ???


in the server side,

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;
}

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

<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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Wish i can get the 4 points back.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...